Exception: Plaid::Errors::ResponseError

Inherits:
ApiError
  • Object
show all
Defined in:
lib/plaid/errors/response_error.rb

Defined Under Namespace

Classes: Headers

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg, code:, body: nil, headers: {}) ⇒ ResponseError

Returns a new instance of ResponseError.

Parameters:

  • msg (String) —

    The raw response body.

  • code (Integer) —

    The HTTP status code.

  • body (Object, nil) (defaults to: nil) —

    The response body parsed into the endpoint's declared error type, if any.

  • headers (Hash{String => Array<String>}) (defaults to: {}) —

    The response headers. Stored with lowercase names; lookups ignore capitalization.



69
70
71
72
73
74
# File 'lib/plaid/errors/response_error.rb', line 69

def initialize(msg, code:, body: nil, headers: {})
  @code = code
  @body = body
  @headers = Headers.from(headers)
  super(msg)
end

Instance Attribute Details

#body ⇒ Object (readonly)

Returns the value of attribute body.



62
63
64
# File 'lib/plaid/errors/response_error.rb', line 62

def body
  @body
end

#code ⇒ Object (readonly)

Returns the value of attribute code.



62
63
64
# File 'lib/plaid/errors/response_error.rb', line 62

def code
  @code
end

#headers ⇒ Object (readonly)

Returns the value of attribute headers.



62
63
64
# File 'lib/plaid/errors/response_error.rb', line 62

def headers
  @headers
end

Class Method Details

.load_error_body(code, raw_body, types) ⇒ Object?

Parses a raw error response body into the type declared for its status code.

Parameters:

  • code (Integer)
  • raw_body (String, nil)
  • types (Hash{Integer, Range => Class}) —

    Matchers in priority order.

Returns:

  • (Object, nil) —

    nil when no type matches or the body cannot be parsed.



94
95
96
97
98
99
100
101
102
103
# File 'lib/plaid/errors/response_error.rb', line 94

def self.load_error_body(code, raw_body, types)
  type = types.find { |matcher, _| matcher.is_a?(::Range) ? matcher.cover?(code) : matcher == code }&.last
  return nil if type.nil? || raw_body.to_s.empty?

  begin
    type.load(raw_body)
  rescue StandardError
    nil
  end
end

.subclass_for_code(code) ⇒ Class

Returns the most appropriate error class for the given code.

Returns:

  • (Class)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/plaid/errors/response_error.rb', line 108

def self.subclass_for_code(code)
  case code
  when 300..399
    RedirectError
  when 401
    UnauthorizedError
  when 403
    ForbiddenError
  when 404
    NotFoundError
  when 400..499
    ClientError
  when 503
    ServiceUnavailableError
  when 500..599
    ServerError
  else
    ResponseError
  end
end

Instance Method Details

#header(name) ⇒ String?

Returns the first value of a response header, or nil when the response did not include it.

Parameters:

  • name (String) —

    The header name, in any capitalization (e.g. "Retry-After").

Returns:

  • (String, nil)


80
81
82
# File 'lib/plaid/errors/response_error.rb', line 80

def header(name)
  Array(headers[name]).first
end

#inspect ⇒ Object



84
85
86
# File 'lib/plaid/errors/response_error.rb', line 84

def inspect
  "#<#{self.class.name} @code=#{code} @body=#{message}>"
end