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 APIErrors < StandardError
16 attr_reader :errors
17
18 def initialize(errors)
19 super(errors.map { |e| e[:description] }.join("\n"))
20 @errors = errors
21 end
22 end
23
24 class Client
25 # Expose useful error messages from the API instead of hiding them
26 def check_response(response)
27 parsed_body = parse_xml(response.body || "")
28 return parsed_body unless response.status >= 400
29 raise APIError.new(**parsed_body[:error]) if parsed_body&.key?(:error)
30 raise APIErrors, parsed_body[:errors] if parsed_body&.key?(:errors)
31
32 raise Errors::GenericError.new(
33 response.status, response.reason_phrase, response.headers, nil
34 )
35 end
36 end
37end