-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance
parse_response
Method to Improve Error Handling and Logging (…
- Loading branch information
1 parent
7ca3887
commit 7141ca3
Showing
7 changed files
with
267 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
module CheckoutSdk | ||
class CheckoutException < StandardError | ||
end | ||
class CheckoutException < StandardError; end | ||
|
||
class CheckoutArgumentException < CheckoutException; end | ||
|
||
class CheckoutAuthorizationException < CheckoutException | ||
def self.invalid_authorization(authorization_type) | ||
CheckoutAuthorizationException.new("Operation requires #{authorization_type} authorization type") | ||
new("Operation requires #{authorization_type} authorization type.") | ||
end | ||
|
||
def self.invalid_key(key_type) | ||
CheckoutAuthorizationException.new("#{key_type} is required for this operation.") | ||
new("#{key_type} is required for this operation.") | ||
end | ||
end | ||
|
||
class CheckoutApiException < CheckoutException | ||
attr_reader :http_metadata, :error_details | ||
|
||
def initialize(response) | ||
def initialize(response, message = nil) | ||
@http_metadata = CheckoutUtils.map_to_http_metadata(response) | ||
if !http_metadata.body.nil? && http_metadata.body != '' | ||
@error_details = JSON.parse(http_metadata.body, object_class: OpenStruct) | ||
@error_details = parse_error_details(http_metadata.body) | ||
super(message || build_error_message) | ||
end | ||
|
||
private | ||
|
||
def parse_error_details(body) | ||
return if body.nil? || body.empty? | ||
|
||
JSON.parse(body, object_class: OpenStruct) | ||
rescue JSON::ParserError | ||
nil | ||
end | ||
|
||
def build_error_message | ||
message = "The API response status code (#{http_metadata.status_code}) does not indicate success." | ||
if @error_details && !@error_details.to_h.empty? | ||
details = @error_details.to_h.map { |key, value| "#{key}: #{value}" }.join(', ') | ||
message += " Details: #{details}." | ||
end | ||
super("The API response status code (#{http_metadata.status_code}) does not indicate success.") | ||
message | ||
end | ||
end | ||
end |
Oops, something went wrong.