~singpolyma/sgx-jmp

sgx-jmp/lib/bandwidth_iris_patch.rb -rw-r--r-- 1.2 KiB
94298f5dStephen Paul Weber Refactor alt top up to use FormTemplate 2 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
38
39
40
41
42
43
44
45
46
47
48
49
# frozen_string_literal: true

require "ruby-bandwidth-iris"

# rubocop:disable Metrics/AbcSize
module BandwidthIris
	class APIError < StandardError
		attr_reader :code

		def self.for_response_status(description:, error_code:)
			new(description: description, code: error_code)
		end

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

	class APIErrors < StandardError
		attr_reader :errors

		def initialize(errors)
			errors = [errors] unless errors.is_a?(Array)
			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)

			if parsed_body&.key?(:response_status)
				raise APIError.for_response_status(**parsed_body[:response_status])
			end

			raise APIErrors, parsed_body[:errors] if parsed_body&.key?(:errors)

			raise Errors::GenericError.new(
				response.status, response.reason_phrase, response.headers, nil
			)
		end
	end
end
# rubocop:enable Metrics/AbcSize