Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion chargebee.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Gem::Specification.new do |s|
lib/chargebee/models/usage_file.rb
lib/chargebee/models/virtual_bank_account.rb
lib/chargebee/models/webhook_endpoint.rb
lib/chargebee/nativeRequest.rb
lib/chargebee/native_request.rb
lib/chargebee/request.rb
lib/chargebee/rest.rb
lib/chargebee/result.rb
Expand Down
2 changes: 1 addition & 1 deletion lib/chargebee.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require File.dirname(__FILE__) + '/chargebee/environment'
require File.dirname(__FILE__) + '/chargebee/nativeRequest'
require File.dirname(__FILE__) + '/chargebee/native_request'
require File.dirname(__FILE__) + '/chargebee/util'
require File.dirname(__FILE__) + '/chargebee/request'
require File.dirname(__FILE__) + '/chargebee/result'
Expand Down
2 changes: 2 additions & 0 deletions lib/chargebee/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def initialize(message=nil,original_error = nil)
end
end

class ForbiddenError < Error; end

class IOError < Error; end

class APIError < Error
Expand Down
23 changes: 15 additions & 8 deletions lib/chargebee/nativeRequest.rb → lib/chargebee/native_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,26 +167,33 @@ def self.handle_json_error(rbody, e)
end
end

# Handle errors returned by the ChargeBee API.
#
# @param rcode [Integer] HTTP status code.
# @param rbody [String] HTTP response body.
#
# @return [ChargeBee::Error] Appropriate ChargeBee error object.
def self.handle_for_error(rcode, rbody)
return Error.new("No response returned by ChargeBee API. HTTP status code: #{rcode}") if rcode == 204
return ForbiddenError.new("Access forbidden. You do not have permission to access this resource.") if rcode == 403
begin
error_obj = JSON.parse(rbody)
error_obj = Util.symbolize_keys(error_obj)
rescue Exception => e
raise Error.new("Error response not in JSON format. The http status code is #{rcode} \n #{rbody.inspect}", e)
return Error.new("Error response not in JSON format. The http status code is #{rcode} \n #{rbody.inspect}", e)
end
type = error_obj[:type]
case type

case error_obj[:type]
when "payment"
raise PaymentError.new(rcode, error_obj)
PaymentError.new(rcode, error_obj)
when "operation_failed"
raise OperationFailedError.new(rcode, error_obj)
OperationFailedError.new(rcode, error_obj)
when "invalid_request"
raise InvalidRequestError.new(rcode, error_obj)
InvalidRequestError.new(rcode, error_obj)
when "ubb_batch_ingestion_invalid_request"
raise UbbBatchIngestionInvalidRequestError.new(rcode, error_obj)
UbbBatchIngestionInvalidRequestError.new(rcode, error_obj)
else
raise APIError.new(rcode, error_obj)
APIError.new(rcode, error_obj)
end
end

Expand Down
13 changes: 13 additions & 0 deletions spec/chargebee/native_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ module ChargeBee
end
end

it "raises ForbiddenError for 403 status code" do
stub_request(:get, "https://dummy.chargebee.com/test").to_return(
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",
status: 403
)

expect {
NativeRequest.request(:get, "/test", env)
}.to raise_error(ForbiddenError) do |err|
expect(err.message).to eq("Access forbidden. You do not have permission to access this resource.")
end
end

it "retries once on HTTP 503 and succeeds on second attempt" do
stub_request(:get, "https://dummy.chargebee.com/test")
.to_return({ status: 503, body: "temporary error" },
Expand Down