~singpolyma/sgx-jmp

ref: 9827b8b48336d249637f88d6921d613ad12b16f1 sgx-jmp/lib/bandwidth_iris_patch.rb -rw-r--r-- 868 bytes
9827b8b4Stephen Paul Weber Allow finishing adming command 11 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