Skip to content

Commit 26559d6

Browse files
authored
Merge pull request #105 from iubenda/v2.63.0-custom-error-for-403-response-code
Add `ForbiddenError` exception for 403 HTTP responses
2 parents 9337690 + d6eb379 commit 26559d6

5 files changed

Lines changed: 32 additions & 10 deletions

File tree

chargebee.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ Gem::Specification.new do |s|
140140
lib/chargebee/models/usage_file.rb
141141
lib/chargebee/models/virtual_bank_account.rb
142142
lib/chargebee/models/webhook_endpoint.rb
143-
lib/chargebee/nativeRequest.rb
143+
lib/chargebee/native_request.rb
144144
lib/chargebee/request.rb
145145
lib/chargebee/rest.rb
146146
lib/chargebee/result.rb

lib/chargebee.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require File.dirname(__FILE__) + '/chargebee/environment'
2-
require File.dirname(__FILE__) + '/chargebee/nativeRequest'
2+
require File.dirname(__FILE__) + '/chargebee/native_request'
33
require File.dirname(__FILE__) + '/chargebee/util'
44
require File.dirname(__FILE__) + '/chargebee/request'
55
require File.dirname(__FILE__) + '/chargebee/result'

lib/chargebee/errors.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ def initialize(message=nil,original_error = nil)
88
end
99
end
1010

11+
class ForbiddenError < Error; end
12+
1113
class IOError < Error; end
1214

1315
class APIError < Error
Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,26 +167,33 @@ def self.handle_json_error(rbody, e)
167167
end
168168
end
169169

170+
# Handle errors returned by the ChargeBee API.
171+
#
172+
# @param rcode [Integer] HTTP status code.
173+
# @param rbody [String] HTTP response body.
174+
#
175+
# @return [ChargeBee::Error] Appropriate ChargeBee error object.
170176
def self.handle_for_error(rcode, rbody)
171177
return Error.new("No response returned by ChargeBee API. HTTP status code: #{rcode}") if rcode == 204
178+
return ForbiddenError.new("Access forbidden. You do not have permission to access this resource.") if rcode == 403
172179
begin
173180
error_obj = JSON.parse(rbody)
174181
error_obj = Util.symbolize_keys(error_obj)
175182
rescue Exception => e
176-
raise Error.new("Error response not in JSON format. The http status code is #{rcode} \n #{rbody.inspect}", e)
183+
return Error.new("Error response not in JSON format. The http status code is #{rcode} \n #{rbody.inspect}", e)
177184
end
178-
type = error_obj[:type]
179-
case type
185+
186+
case error_obj[:type]
180187
when "payment"
181-
raise PaymentError.new(rcode, error_obj)
188+
PaymentError.new(rcode, error_obj)
182189
when "operation_failed"
183-
raise OperationFailedError.new(rcode, error_obj)
190+
OperationFailedError.new(rcode, error_obj)
184191
when "invalid_request"
185-
raise InvalidRequestError.new(rcode, error_obj)
192+
InvalidRequestError.new(rcode, error_obj)
186193
when "ubb_batch_ingestion_invalid_request"
187-
raise UbbBatchIngestionInvalidRequestError.new(rcode, error_obj)
194+
UbbBatchIngestionInvalidRequestError.new(rcode, error_obj)
188195
else
189-
raise APIError.new(rcode, error_obj)
196+
APIError.new(rcode, error_obj)
190197
end
191198
end
192199

spec/chargebee/native_request_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ module ChargeBee
141141
end
142142
end
143143

144+
it "raises ForbiddenError for 403 status code" do
145+
stub_request(:get, "https://dummy.chargebee.com/test").to_return(
146+
body: "<html>\r\n<head><title>403 Forbidden</title></head>\r\n<body>\r\n<center><h1>403 Forbidden</h1></center>\r\n</body>\r\n</html>\r\n",
147+
status: 403
148+
)
149+
150+
expect {
151+
NativeRequest.request(:get, "/test", env)
152+
}.to raise_error(ForbiddenError) do |err|
153+
expect(err.message).to eq("Access forbidden. You do not have permission to access this resource.")
154+
end
155+
end
156+
144157
it "retries once on HTTP 503 and succeeds on second attempt" do
145158
stub_request(:get, "https://dummy.chargebee.com/test")
146159
.to_return({ status: 503, body: "temporary error" },

0 commit comments

Comments
 (0)