Module: ErrorHandlingResourceable

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/qrapi/error_handling_resourceable.rb', line 2

def self.included(base)
  base.send(:resources) do
    default_handler do |response|
      if (200...299).include?(response.status)
        next
      elsif response.status == 429
        error = QRAPI::RateLimitReached.new("#{response.status}: #{response.body}")
        error.limit = response.headers["RateLimit-Limit"]
        error.remaining = response.headers["RateLimit-Remaining"]
        error.reset_at = response.headers["RateLimit-Reset"]
        raise error
      else
        body = JSON.parse(response.body)
        error_message = body["meta"]["error"]["errorMessage"]
        error_code = body["meta"]["error"]["errorCode"]
        raise QRAPI::Error.new("Status: #{response.status} Error: #{error_code} - #{error_message}")
      end
    end
  end
end