Exception: Gcloud::Search::ApiError

Inherits:
Error
  • Object
show all
Defined in:
lib/gcloud/search/errors.rb

Overview

Raised when an API call is not successful.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, code, errors = []) ⇒ ApiError

:nodoc:



36
37
38
39
40
# File 'lib/gcloud/search/errors.rb', line 36

def initialize message, code, errors = [] #:nodoc:
  super message
  @code   = code
  @errors = errors
end

Instance Attribute Details

#codeObject (readonly)

The code of the error.



30
31
32
# File 'lib/gcloud/search/errors.rb', line 30

def code
  @code
end

#errorsObject (readonly)

The errors encountered.



34
35
36
# File 'lib/gcloud/search/errors.rb', line 34

def errors
  @errors
end

Class Method Details

.from_response(resp) ⇒ Object

:nodoc:



42
43
44
45
46
47
48
49
50
51
# File 'lib/gcloud/search/errors.rb', line 42

def self.from_response resp #:nodoc:
  data = JSON.parse resp.body
  if data["error"]
    from_response_data data["error"]
  else
    from_response_status resp
  end
rescue JSON::ParserError
  from_response_status resp
end

.from_response_data(error) ⇒ Object

:nodoc:



53
54
55
# File 'lib/gcloud/search/errors.rb', line 53

def self.from_response_data error #:nodoc:
  new error["message"], error["code"], error["errors"]
end

.from_response_status(resp) ⇒ Object

:nodoc:



57
58
59
60
61
62
63
64
# File 'lib/gcloud/search/errors.rb', line 57

def self.from_response_status resp #:nodoc:
  if resp.status == 404
    new "#{resp.body}: #{resp.request.uri.request_uri}",
        resp.status
  else
    new resp.body, resp.status
  end
end