Exception: PactasItero::Error

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

Overview

Custom error class for rescuing from all Pactas errors

Direct Known Subclasses

ClientError, ServerError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Error

Returns a new instance of Error.



35
36
37
38
# File 'lib/pactas_itero/error.rb', line 35

def initialize(response = nil)
  @response = response
  super(build_error_message)
end

Class Method Details

.error_for401(headers) ⇒ Object

Returns most appropriate error for 401 HTTP status code



42
43
44
45
46
47
48
# File 'lib/pactas_itero/error.rb', line 42

def self.error_for401(headers)
  if PactasItero::OneTimePasswordRequired.required_header(headers)
    PactasItero::OneTimePasswordRequired
  else
    PactasItero::Unauthorized
  end
end

.error_for403(body) ⇒ Object

Returns most appropriate error for 403 HTTP status code



52
53
54
55
56
57
58
59
60
61
# File 'lib/pactas_itero/error.rb', line 52

def self.error_for403(body)
  case body
  when /rate limit exceeded/i
    PactasItero::TooManyRequests
  when /login attempts exceeded/i
    PactasItero::TooManyLoginAttempts
  else
    PactasItero::Forbidden
  end
end

.from_response(response) ⇒ PactasItero::Error

Returns the appropriate PactasItero::Error sublcass based on status and response message

Parameters:

  • response (Hash)

    HTTP response

Returns:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pactas_itero/error.rb', line 11

def self.from_response(response)
  status = response[:status].to_i
  body = response[:body].to_s
  headers = response[:response_headers]

  klass = case status
  when 400 then PactasItero::BadRequest
  when 401 then error_for401(headers)
  when 403 then error_for403(body)
  when 404 then PactasItero::NotFound
  when 406 then PactasItero::NotAcceptable
  when 409 then PactasItero::Conflict
  when 415 then PactasItero::UnsupportedMediaType
  when 422 then PactasItero::UnprocessableEntity
  when 400..499 then PactasItero::ClientError
  when 500 then PactasItero::InternalServerError
  when 501 then PactasItero::NotImplemented
  when 502 then PactasItero::BadGateway
  when 503 then PactasItero::ServiceUnavailable
  when 500..599 then PactasItero::ServerError
  end
  klass&.new(response)
end

Instance Method Details

#errorsArray<Hash>

Array of validation errors

Returns:

  • (Array<Hash>)

    Error info



65
66
67
68
69
70
71
# File 'lib/pactas_itero/error.rb', line 65

def errors
  if data.is_a?(Hash)
    data[:errors] || []
  else
    []
  end
end