Skip to content

Commit

Permalink
Add response to Pub/Sub Error
Browse files Browse the repository at this point in the history
Not every error returned has status, code, and errors in the response.
Guard against this by raising Gcloud::Pubsub::Error with response attr.
We want to avoid unhandled errors while raising a typed error. :)
  • Loading branch information
blowmage committed Jul 20, 2015
1 parent 58cbb74 commit 45b1e7a
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions lib/gcloud/pubsub/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ module Pubsub
#
# Base Pub/Sub exception class.
class Error < Gcloud::Error
##
# The response object of the failed HTTP request.
attr_reader :response

def self.from_response resp #:nodoc:
new.tap do |e|
e.response = resp
end
end
end

##
Expand All @@ -29,23 +38,30 @@ class Error < Gcloud::Error
class ApiError < Error
##
# The code of the error.
attr_reader :code
def code
response.data["error"]["code"]
rescue
nil
end

##
# The errors encountered.
attr_reader :errors
def errors
response.data["error"]["errors"]
rescue
[]
end

def initialize message, code, errors
def initialize message, response
super message
@code = code
@errors = errors
@response = response
end

def self.from_response resp #:nodoc:
klass = klass_for resp.data["error"]["status"]
klass.new resp.data["error"]["message"],
resp.data["error"]["code"],
resp.data["error"]["errors"]
klass.new resp.data["error"]["message"], resp
rescue
Gcloud::Pubsub::Error.from_response resp
end

def self.klass_for status
Expand All @@ -61,14 +77,14 @@ def self.klass_for status
##
# = AlreadyExistsError
#
# Raised when Pub/Sub returns an ALREADY_EXISTS error.
# Raised when Pub/Sub returns an +ALREADY_EXISTS+ error.
class AlreadyExistsError < ApiError
end

##
# = NotFoundError
#
# Raised when Pub/Sub returns a NOT_FOUND error.
# Raised when Pub/Sub returns a +NOT_FOUND+ error.
class NotFoundError < ApiError
end
end
Expand Down

0 comments on commit 45b1e7a

Please sign in to comment.