class NgpVan::Error

Custom error class for rescuing from NgpVan errors.

Attributes

body[R]
errors[R]
response[R]
status[R]

Public Class Methods

from_response(response) click to toggle source

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

@param [Hash] response HTTP response @return [NgpVan::Error]

# 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
new(response = nil) click to toggle source
Calls superclass method
# 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

Private Instance Methods

build_error() click to toggle source
# File lib/ngp_van/error.rb, line 55
def build_error
  return nil if response.nil?

  {
    status: status,
    errors: errors
  }
end