Exception: NgpVan::Error

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

Overview

Custom error class for rescuing from NgpVan errors.

Direct Known Subclasses

ClientError, ServerError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Error

Returns a new instance of Error.



44
45
46
47
48
49
50
51
# File 'lib/ngp_van/error.rb', line 44

def initialize(response = nil)
  @response = response
  @body = ::JSON.parse(response[:body])
  @status = response[:status]
  @errors = body.delete('errors')

  super(build_error)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/ngp_van/error.rb', line 6

def body
  @body
end

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/ngp_van/error.rb', line 6

def errors
  @errors
end

#responseObject (readonly)

Returns the value of attribute response.



6
7
8
# File 'lib/ngp_van/error.rb', line 6

def response
  @response
end

#statusObject (readonly)

Returns the value of attribute status.



6
7
8
# File 'lib/ngp_van/error.rb', line 6

def status
  @status
end

Class Method Details

.from_response(response) ⇒ NgpVan::Error

rubocop:disable Metrics/MethodLength, Metrics/AbcSize, rubocop:disable Style/CyclomaticComplexity Returns a NgpVan::Error subclass, depending on status and response message.

Parameters:

  • response (Hash)

    HTTP response

Returns:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ngp_van/error.rb', line 15

def self.from_response(response)
  status = response[:status].to_i

  error_klass =
    case status
    when 400 then NgpVan::BadRequest
    when 401 then NgpVan::Unauthorized
    when 403 then NgpVan::Forbidden
    when 405 then NgpVan::MethodNotAllowed
    when 406 then NgpVan::NotAcceptable
    when 408 then NgpVan::RequestTimeout
    when 409 then NgpVan::Conflict
    when 413 then NgpVan::RequestEntityTooLarge
    when 415 then NgpVan::UnsupportedMediaType
    when 422 then NgpVan::UnprocessableEntity
    when 429 then NgpVan::TooManyRequests
    when 404 then NgpVan::NotFound
    when 400..499 then NgpVan::ClientError
    when 500 then NgpVan::InternalServerError
    when 501 then NgpVan::NotImplemented
    when 502 then NgpVan::BadGateway
    when 503 then NgpVan::ServiceUnavailable
    when 504 then NgpVan::GatewayTimeout
    when 500..599 then NgpVan::ServerError
    end

  error_klass.new(response) if error_klass
end