Skip to content

Commit

Permalink
Pass response to exception
Browse files Browse the repository at this point in the history
  • Loading branch information
c960657 committed Nov 11, 2024
1 parent 17680dc commit 58fce93
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
10 changes: 9 additions & 1 deletion lib/http/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ class ResponseError < Error; end
class StateError < ResponseError; end

# When status code indicates an error
class StatusError < ResponseError; end
class StatusError < ResponseError
attr_reader :response

def initialize(response)
@response = response

super("Unexpected status code #{response.code}")
end
end

# Generic Timeout error
class TimeoutError < Error; end
Expand Down
7 changes: 3 additions & 4 deletions lib/http/features/raise_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ def initialize(ignore: [])
end

def wrap_response(response)
if response.code >= 400 && !@ignore.include?(response.code)
raise HTTP::StatusError, "Unexpected status code #{response.code}"
end
return response if response.code < 400
return response if @ignore.include?(response.code)

response
raise HTTP::StatusError, response
end

HTTP::Options.register_feature(:raise_error, self)
Expand Down

0 comments on commit 58fce93

Please sign in to comment.