Skip to content

Commit

Permalink
Error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
olimart committed Dec 1, 2023
1 parent 2909b85 commit b2bb1ed
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
18 changes: 9 additions & 9 deletions lib/queenbee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,32 +110,32 @@ def self.handle_api_error(rcode, rbody)
raise general_api_error(rcode, rbody)
end

case rcode
case rcode.to_i
when 400, 404, 422
raise invalid_request_error "Invalid request", rcode, rbody, error_obj
raise invalid_request_error("Invalid request", rcode, rbody, error_obj).to_s
when 401
raise authentication_error "Unauthorized", rcode, rbody, error_obj
raise authentication_error("Unauthorized", rcode, rbody, error_obj).to_s
when 500
raise api_error "Server returned an error", rcode, rbody, error_obj
raise api_error("Server returned an error", rcode, rbody, error_obj).to_s
else
raise api_error "An error has occurred", rcode, rbody, error_obj
raise api_error("An error has occurred", rcode, rbody, error_obj).to_s
end
end

def self.invalid_request_error(message, rcode, rbody, error_obj)
InvalidRequestError.new(message, rcode, rbody, error_obj)
InvalidRequestError.new(message, nil, rcode, rbody, error_obj)
end

def self.authentication_error(message, rcode, rbody, error_obj)
AuthenticationError.new(message, rcode, rbody, error_obj)
AuthenticationError.new(message, nil, rcode, rbody, error_obj)
end

def self.api_error(message, rcode, rbody, error_obj)
APIError.new(message, rcode, rbody, error_obj)
APIError.new(message, nil, rcode, rbody, error_obj)
end

def self.general_api_error(rcode, rbody)
APIError.new("Invalid response object from API: #{rbody.inspect} " +
"(HTTP response code was #{rcode})", rcode, rbody)
"(HTTP response code was #{rcode})", nil, rcode, rbody)
end
end
2 changes: 1 addition & 1 deletion lib/queenbee/errors/invalid_request_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ def initialize(message, param, http_status=nil, http_body=nil, json_body=nil)
@param = param
end
end
end
end
13 changes: 10 additions & 3 deletions lib/queenbee/errors/queenbee_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ def initialize(message=nil, http_status=nil, http_body=nil, json_body=nil)
end

def to_s
status_string = @http_status.nil? ? "" : "(Status #{@http_status}) "
"#{status_string}#{@message}"
[
status_string,
json_body,
message
].compact.join(" - ")
end

def status_string
http_status.nil? ? "" : "(Status #{http_status})"
end
end
end
end

0 comments on commit b2bb1ed

Please sign in to comment.