Skip to content

Commit

Permalink
Refactor error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Aug 3, 2023
1 parent cedc0dc commit 86b536f
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions lib/x/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,32 +164,30 @@ def add_user_agent(request)

# HTTP client error handler
class ErrorHandler
HTTP_STATUS_HANDLERS = {
Net::HTTPOK => :handle_success_response,
Net::HTTPBadRequest => :handle_bad_request_response,
Net::HTTPForbidden => :handle_forbidden_response,
Net::HTTPUnauthorized => :handle_unauthorized_response,
Net::HTTPNotFound => :handle_not_found_response,
Net::HTTPTooManyRequests => :handle_too_many_requests_response,
Net::HTTPInternalServerError => :handle_server_error_response,
Net::HTTPServiceUnavailable => :handle_service_unavailable_response
}.freeze

def initialize(response)
@response = response
end

def handle
handler_method = HTTP_STATUS_HANDLERS[@response.class]
if handler_method
send(handler_method)
else
handle_unexpected_response
end
send(handler_method)
end

private

def handler_method # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
case @response
when Net::HTTPOK then :handle_success_response
when Net::HTTPBadRequest then :handle_bad_request_response
when Net::HTTPForbidden then :handle_forbidden_response
when Net::HTTPUnauthorized then :handle_unauthorized_response
when Net::HTTPNotFound then :handle_not_found_response
when Net::HTTPTooManyRequests then :handle_too_many_requests_response
when Net::HTTPInternalServerError then :handle_server_error_response
when Net::HTTPServiceUnavailable then :handle_service_unavailable_response
else :handle_unexpected_response
end
end

def handle_success_response
JSON.parse(@response.body)
end
Expand Down

0 comments on commit 86b536f

Please sign in to comment.