Class: Response::RaiseElvantoError

Inherits:
Middleware
  • Object
show all
Defined in:
lib/elvanto/response/elvanto_exception_middleware.rb

Constant Summary collapse

CATEGORY_CODE_MAP =
{
    50 => ElvantoAPI::Unauthorized,
    102 => ElvantoAPI::Unauthorized,
    250 => ElvantoAPI::BadRequest,
    404 => ElvantoAPI::NotFound,
    500 => ElvantoAPI::InternalError
}
HTTP_STATUS_CODES =
{
    401 => ElvantoAPI::Unauthorized,
    400 => ElvantoAPI::BadRequest,
    404 => ElvantoAPI::NotFound,
    500 => ElvantoAPI::InternalError    
}

Instance Method Summary collapse

Instance Method Details

#on_complete(response) ⇒ Object

Raises:

  • (error_class)


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/elvanto/response/elvanto_exception_middleware.rb', line 24

def on_complete(response)

  status_code = response[:status].to_i
  if response[:body] != nil && response[:body]['error']
    category_code = response[:body]['error']["code"]
  else
    category_code = nil
  end

  error_class = CATEGORY_CODE_MAP[category_code] || HTTP_STATUS_CODES[status_code]
  raise error_class.new(response[:body]) if error_class

end