1# frozen_string_literal: true
2
3require "ruby-bandwidth-iris"
4
5module BandwidthIris
6 class APIError < StandardError
7 attr_reader :code
8
9 def initialize(description:, code: nil)
10 super(description)
11 @code = code
12 end
13 end
14
15 class Client
16 # Expose useful error messages from the API instead of hiding them
17 def check_response(response)
18 parsed_body = parse_xml(response.body || "")
19 return parsed_body unless response.status >= 400
20 raise APIError.new(**parsed_body[:error]) if parsed_body.key?(:error)
21
22 raise Errors::GenericError.new(
23 "", "Http code #{response.status}", response.status
24 )
25 end
26 end
27end