Exception: Restify::ResponseError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/restify/error.rb

Overview

A ResponseError is returned on a non-successful response from server. Usually it will either be a ClientError or a ServerError.

Direct Known Subclasses

ClientError, ServerError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ ResponseError

Returns a new instance of ResponseError.



56
57
58
59
60
# File 'lib/restify/error.rb', line 56

def initialize(response)
  @response = response
  super "#{response.message} (#{response.code}) for `#{response.uri}':\n" \
        "  #{errors.inspect}"
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



21
22
23
# File 'lib/restify/error.rb', line 21

def response
  @response
end

Class Method Details

.from_code(response) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/restify/error.rb', line 23

def self.from_code(response)
  case response.code
    when 400
      BadRequest.new(response)
    when 401
      Unauthorized.new(response)
    when 404
      NotFound.new(response)
    when 406
      NotAcceptable.new(response)
    when 410
      Gone.new(response)
    when 422
      UnprocessableEntity.new(response)
    when 429
      TooManyRequests.new(response)
    when 400...500
      ClientError.new(response)
    when 500
      InternalServerError.new(response)
    when 502
      BadGateway.new(response)
    when 503
      ServiceUnavailable.new(response)
    when 504
      GatewayTimeout.new(response)
    when 500...600
      ServerError.new(response)
    else
      raise "Unknown response code: #{response.code}"
  end
end

Instance Method Details

#codeFixnum

Return response status code.

Returns:

  • (Fixnum)

    Response status code.

See Also:



76
77
78
# File 'lib/restify/error.rb', line 76

def code
  response.code
end

#errorsObject

Return hash or array of errors if response included such a thing otherwise it returns nil.



83
84
85
86
87
88
89
90
91
# File 'lib/restify/error.rb', line 83

def errors
  if response.decoded_body
    response.decoded_body['errors'] ||
      response.decoded_body[:errors] ||
      response.decoded_body
  else
    response.body
  end
end

#statusSymbol

Return response status.

Returns:

  • (Symbol)

    Response status.

See Also:



67
68
69
# File 'lib/restify/error.rb', line 67

def status
  response.status
end