~singpolyma/sgx-jmp

ref: f571b5e726750cf8c4523c22d77e110194c3e16a sgx-jmp/lib/bandwidth_iris_patch.rb -rw-r--r-- 868 bytes
f571b5e7Stephen Paul Weber Postgres#query_one 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true

require "ruby-bandwidth-iris"

module BandwidthIris
	class APIError < StandardError
		attr_reader :code

		def initialize(description:, code: nil)
			super(description)
			@code = code
		end
	end

	class APIErrors < StandardError
		attr_reader :errors

		def initialize(errors)
			super(errors.map { |e| e[:description] }.join("\n"))
			@errors = errors
		end
	end

	class Client
		# Expose useful error messages from the API instead of hiding them
		def check_response(response)
			parsed_body = parse_xml(response.body || "")
			return parsed_body unless response.status >= 400
			raise APIError.new(**parsed_body[:error]) if parsed_body&.key?(:error)
			raise APIErrors, parsed_body[:errors] if parsed_body&.key?(:errors)

			raise Errors::GenericError.new(
				"", "Http code #{response.status}", response.status
			)
		end
	end
end