Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ lib/gcloud/datastore/connection.rb
lib/gcloud/datastore/credentials.rb
lib/gcloud/datastore/dataset.rb
lib/gcloud/datastore/entity.rb
lib/gcloud/datastore/errors.rb
lib/gcloud/datastore/key.rb
lib/gcloud/datastore/list.rb
lib/gcloud/datastore/proto.rb
Expand Down
8 changes: 4 additions & 4 deletions gcloud.gemspec
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# -*- encoding: utf-8 -*-
# stub: gcloud 0.0.1.20141114161251 ruby lib
# stub: gcloud 0.0.1.20141119145908 ruby lib

Gem::Specification.new do |s|
s.name = "gcloud"
s.version = "0.0.1.20141114161251"
s.version = "0.0.1.20141119145908"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.require_paths = ["lib"]
s.authors = ["Silvano Luciani", "Mike Moore"]
s.date = "2014-11-14"
s.date = "2014-11-19"
s.description = "Gcloud is the official library for interacting with Google Cloud."
s.email = ["silvano@google.com", "mike@blowmage.com"]
s.extra_rdoc_files = ["CHANGELOG.md", "CONTRIBUTING.md", "Manifest.txt", "README.md"]
s.files = [".gemtest", ".rubocop.yml", "CHANGELOG.md", "CONTRIBUTING.md", "LICENSE", "Manifest.txt", "README.md", "Rakefile", "gcloud.gemspec", "lib/gcloud.rb", "lib/gcloud/datastore.rb", "lib/gcloud/datastore/connection.rb", "lib/gcloud/datastore/credentials.rb", "lib/gcloud/datastore/dataset.rb", "lib/gcloud/datastore/entity.rb", "lib/gcloud/datastore/key.rb", "lib/gcloud/datastore/list.rb", "lib/gcloud/datastore/proto.rb", "lib/gcloud/datastore/query.rb", "lib/gcloud/datastore/transaction.rb", "lib/gcloud/proto/datastore_v1.pb.rb", "lib/gcloud/version.rb", "rakelib/console.rake", "rakelib/proto.rake", "rakelib/rubocop.rake", "rakelib/test.rake", "regression/data/index.yaml", "regression/datastore/test_datastore.rb", "regression/datastore_helper.rb", "test/gcloud/datastore/proto/test_cursor.rb", "test/gcloud/datastore/proto/test_direction.rb", "test/gcloud/datastore/proto/test_operator.rb", "test/gcloud/datastore/proto/test_value.rb", "test/gcloud/datastore/test_connection.rb", "test/gcloud/datastore/test_credentials.rb", "test/gcloud/datastore/test_dataset.rb", "test/gcloud/datastore/test_entity.rb", "test/gcloud/datastore/test_key.rb", "test/gcloud/datastore/test_query.rb", "test/gcloud/datastore/test_transaction.rb", "test/gcloud/test_version.rb", "test/helper.rb"]
s.files = [".gemtest", ".rubocop.yml", "CHANGELOG.md", "CONTRIBUTING.md", "LICENSE", "Manifest.txt", "README.md", "Rakefile", "gcloud.gemspec", "lib/gcloud.rb", "lib/gcloud/datastore.rb", "lib/gcloud/datastore/connection.rb", "lib/gcloud/datastore/credentials.rb", "lib/gcloud/datastore/dataset.rb", "lib/gcloud/datastore/entity.rb", "lib/gcloud/datastore/errors.rb", "lib/gcloud/datastore/key.rb", "lib/gcloud/datastore/list.rb", "lib/gcloud/datastore/proto.rb", "lib/gcloud/datastore/query.rb", "lib/gcloud/datastore/transaction.rb", "lib/gcloud/proto/datastore_v1.pb.rb", "lib/gcloud/version.rb", "rakelib/console.rake", "rakelib/proto.rake", "rakelib/rubocop.rake", "rakelib/test.rake", "regression/data/index.yaml", "regression/datastore/test_datastore.rb", "regression/datastore_helper.rb", "test/gcloud/datastore/proto/test_cursor.rb", "test/gcloud/datastore/proto/test_direction.rb", "test/gcloud/datastore/proto/test_operator.rb", "test/gcloud/datastore/proto/test_value.rb", "test/gcloud/datastore/test_connection.rb", "test/gcloud/datastore/test_credentials.rb", "test/gcloud/datastore/test_dataset.rb", "test/gcloud/datastore/test_entity.rb", "test/gcloud/datastore/test_key.rb", "test/gcloud/datastore/test_query.rb", "test/gcloud/datastore/test_transaction.rb", "test/gcloud/test_version.rb", "test/helper.rb"]
s.homepage = "http://googlecloudplatform.github.io/gcloud-ruby/"
s.licenses = ["Apache-2.0"]
s.rdoc_options = ["--main", "README.md", "--exclude", "lib/gcloud/proto/"]
Expand Down
7 changes: 7 additions & 0 deletions lib/gcloud.rb
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
require "gcloud/version"

module Gcloud
##
# Standard Gcloud exception class.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

class Error < StandardError
end
end
1 change: 1 addition & 0 deletions lib/gcloud/datastore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

require "gcloud"
require "gcloud/datastore/errors"
require "gcloud/datastore/dataset"
require "gcloud/datastore/transaction"
require "gcloud/datastore/credentials"
Expand Down
4 changes: 2 additions & 2 deletions lib/gcloud/datastore/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ def rpc proto_method, proto_request
@credentials.sign_http_request req
end

# TODO: Raise a proper error if the response is not 2xx (success)
fail response.inspect unless response.success?
fail ConnectionError, proto_method, response unless response.success?

response.body
end

Expand Down
4 changes: 2 additions & 2 deletions lib/gcloud/datastore/credentials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class Credentials # :nodoc:

def initialize keyfile
if keyfile.nil?
fail "You must provide a keyfile to connect with."
fail KeyfileError, "You must provide a keyfile to connect with."
elsif !File.exist?(keyfile)
fail "The keyfile '#{keyfile}' is not a valid file."
fail KeyfileError, "The keyfile '#{keyfile}' is not a valid file."
end

options = JSON.parse(File.read(keyfile))
Expand Down
8 changes: 5 additions & 3 deletions lib/gcloud/datastore/dataset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def dataset_id
# empty_key = Gcloud::Datastore::Key.new "Task"
# task_keys = conn.allocate_ids empty_key, 5
def allocate_ids incomplete_key, count = 1
fail "An incomplete key must be provided." if incomplete_key.complete?
if incomplete_key.complete?
fail Gcloud::Datastore::Error, "An incomplete key must be provided."
end

incomplete_keys = count.times.map { incomplete_key.to_proto }
response = connection.allocate_ids(*incomplete_keys)
Expand Down Expand Up @@ -134,9 +136,9 @@ def transaction
begin
yield tx
tx.commit
rescue
rescue => e
tx.rollback
raise "Transaction failed to commit."
raise TransactionError.new("Transaction failed to commit.", e)
end
end

Expand Down
55 changes: 55 additions & 0 deletions lib/gcloud/datastore/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2014 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require "gcloud"

module Gcloud
module Datastore
##
# Standard Datastore exception class.
class Error < Gcloud::Error
end

##
# Raised when a keyfile is not correct.
class KeyfileError < Gcloud::Datastore::Error
end

##
# Raised when an API call is not successful.
class ApiError < Gcloud::Datastore::Error
##
# The response object of the failed HTTP request.
attr_reader :response

def initialize method, response = nil
super "API call to #{method} was not successful"
@response = response
end
end

##
# General error for Transaction problems.
class TransactionError < Gcloud::Datastore::Error
##
# An error that occurred within the transaction. (optional)
attr_reader :inner

def initialize message, inner = nil
super message
@inner = inner
end
end
end
end
10 changes: 7 additions & 3 deletions lib/gcloud/datastore/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,26 @@ def delete *entities
end

def start
fail "Transaction already opened" unless @id.nil?
fail TransactionError, "Transaction already opened." unless @id.nil?

response = connection.begin_transaction
@id = response.transaction
end

def commit
fail "Cannot commit when not in a transaction" if @id.nil?
if @id.nil?
fail TransactionError, "Cannot commit when not in a transaction."
end

response = connection.commit shared_mutation, @id
auto_id_assign_ids response.mutation_result.insert_auto_id_key
true
end

def rollback
fail "Cannot rollback when not in a transaction" if @id.nil?
if @id.nil?
fail TransactionError, "Cannot rollback when not in a transaction."
end

connection.rollback @id
true
Expand Down
19 changes: 18 additions & 1 deletion test/gcloud/datastore/test_dataset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
it "allocate_ids raises when not given an incomplete key" do
complete_key = Gcloud::Datastore::Key.new "ds-test", 789
complete_key.must_be :complete?
assert_raises RuntimeError do
assert_raises Gcloud::Datastore::Error do
dataset.allocate_ids complete_key
end
end
Expand Down Expand Up @@ -208,4 +208,21 @@
tx.save entity
end
end

it "transaction will wrap errors in TransactionError" do
dataset.connection.expect :begin_transaction,
begin_transaction_response
dataset.connection.expect :rollback, nil, [String]

error = assert_raises Gcloud::Datastore::TransactionError do
dataset.transaction do |tx|
fail "This error should be wrapped by TransactionError."
end
end

error.wont_be :nil?
error.message.must_equal "Transaction failed to commit."
error.inner.wont_be :nil?
error.inner.message.must_equal "This error should be wrapped by TransactionError."
end
end
2 changes: 1 addition & 1 deletion test/gcloud/datastore/test_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

require "helper"
require "gcloud/datastore/entity"
require "gcloud/datastore"

describe Gcloud::Datastore::Entity do

Expand Down
2 changes: 1 addition & 1 deletion test/gcloud/datastore/test_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

require "helper"
require "gcloud/datastore/key"
require "gcloud/datastore"

describe Gcloud::Datastore::Key do

Expand Down
2 changes: 1 addition & 1 deletion test/gcloud/datastore/test_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

require "helper"
require "gcloud/datastore/query"
require "gcloud/datastore"

describe Gcloud::Datastore::Query do
it "can query on kind" do
Expand Down
37 changes: 37 additions & 0 deletions test/gcloud/datastore/test_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,41 @@
transaction.save entity
transaction.rollback
end

describe "error handling" do
it "start will raise if transaction is already open" do
transaction.id.wont_be :nil?
error = assert_raises Gcloud::Datastore::TransactionError do
transaction.start
end
error.wont_be :nil?
error.message.must_equal "Transaction already opened."
end

it "commit will raise if transaction is not open" do
transaction.connection.expect :begin_transaction, begin_transaction_response

transaction.id.wont_be :nil?
transaction.reset!
transaction.id.must_be :nil?
error = assert_raises Gcloud::Datastore::TransactionError do
transaction.commit
end
error.wont_be :nil?
error.message.must_equal "Cannot commit when not in a transaction."
end

it "transaction will raise if transaction is not open" do
transaction.connection.expect :begin_transaction, begin_transaction_response

transaction.id.wont_be :nil?
transaction.reset!
transaction.id.must_be :nil?
error = assert_raises Gcloud::Datastore::TransactionError do
transaction.rollback
end
error.wont_be :nil?
error.message.must_equal "Cannot rollback when not in a transaction."
end
end
end