Last active
August 6, 2018 18:27
-
-
Save rantler/24c544f9915a63a976ff5346af093cde to your computer and use it in GitHub Desktop.
Raise custom exception on request error
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
class ApprovalError < StandardError | |
def initialize(response) | |
@response = response | |
@request = response.request | |
end | |
def inspect | |
"uri: #{@request.uri}, response_body: #{@response.body}" | |
end | |
end | |
class ApprovalBadGatewayError < ApprovalError; end | |
class ApprovalInternalServerError < ApprovalError; end | |
module Fontana | |
module Approval | |
class Workflow | |
def update | |
result = self.class.put( | |
"#{approvals_api_uri}/#{id}", | |
:headers => approval_request_headers, | |
:body => to_json | |
) | |
handle_approval_error(response) unless result.success? | |
# ... | |
def handle_approval_error(response) | |
case response.code | |
when '502' | |
raise ApprovalBadGatewayError.new(response) | |
when '500' | |
raise ApprovalInternalServerError.new(reponse) | |
else | |
raise ApprovalError.new(reponse) | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment