Exception: Revolut::Error

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

Overview

Base Revolut error.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg = nil) ⇒ Error

Returns a new instance of Error.



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

def initialize(msg = nil)
  super(msg)
  @message = msg
end

Class Method Details

.error_class(status) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/revolut/error.rb', line 31

def self.error_class(status)
  case status
  when 400 then Revolut::BadRequest
  when 401 then Revolut::Unauthorized
  when 403 then Revolut::Forbidden
  when 404 then Revolut::NotFound
  when 405 then Revolut::MethodNotAllowed
  when 406 then Revolut::NotAcceptable
  when 429 then Revolut::TooManyRequests
  when 500 then Revolut::InternalServerError
  when 503 then Revolut::ServiceUnavailable
  end
end

.error_message(response) ⇒ String

Returns the appropriate Revolut error message based on response

Parameters:

  • response (Faraday::Env)

    HTTP response.

Returns:

  • (String)

    Revolut error message.



51
52
53
54
55
56
57
58
# File 'lib/revolut/error.rb', line 51

def self.error_message(response)
  return unless response.body.is_a?(Hash)

  message = response.body['message']
  return unless message

  Revolut::Utils.presence(message)
end

.from_response(response) ⇒ Revolut::Error

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

Parameters:

  • response (Faraday::Env)

    HTTP response.

Returns:



22
23
24
25
26
27
28
# File 'lib/revolut/error.rb', line 22

def self.from_response(response)
  status = response.status.to_i
  message = error_message(response)

  klass = error_class(status)
  klass&.new(message)
end

Instance Method Details

#to_sObject

Default error message.



12
13
14
# File 'lib/revolut/error.rb', line 12

def to_s
  @message || super
end