Class: EasyqaApi::Exception

Inherits:
Object
  • Object
show all
Defined in:
lib/easyqa_api/exceptions.rb

Overview

Class for processing exceptions

Defined Under Namespace

Classes: NotFoundError, PermissionError, RequestError, ValidationError

Constant Summary collapse

EXCEPTIONS =

Constant for quickly find exception class by response status

{
  404 => NotFoundError,
  403 => PermissionError,
  400 => RequestError,
  422 => ValidationError
}.freeze

Class Method Summary collapse

Class Method Details

.check_response_status!(response) ⇒ Object

Check response status

Parameters:

  • response (Faraday::Response)

    the response of your request

Raises:

See Also:



35
36
37
38
# File 'lib/easyqa_api/exceptions.rb', line 35

def self.check_response_status!(response)
  raise EXCEPTIONS[response.status], response.body['message'] || retrieve_mess(response.body) \
    if EXCEPTIONS[response.status]
end

.retrieve_mess(response_boddy) ⇒ Object

Retrieve message by response body

Examples:

EasyqaApi::Exception.retrieve_mess({ title: ["can`t be blank"]}) #=> "title: can`t be blank"

Parameters:

  • response_boddy (Hash)


44
45
46
47
48
# File 'lib/easyqa_api/exceptions.rb', line 44

def self.retrieve_mess(response_boddy)
  response_boddy.each_with_object('') do |(key, values), result_message|
    result_message << "#{key}: #{values.join('; ')}\n"
  end
end