Skip to content

Commit

Permalink
Set the failed http response in a custom attribute instead of cause
Browse files Browse the repository at this point in the history
`#cause` in a custom error should return the expected class. This
commit updates the custom value that is useful to store, the response,
to use a specific name rather than overriding `#cause`’s behavior.

Ruby documentation explains that the default expectation of #cause is
that it will return either `an_exception` or `nil`. By setting an
`HTTP::Response` to return from `#cause`, tools utilizing it may expect
to send messages that may fail.

https://ruby-doc.org/3.2.6/Exception.html#method-i-cause

Specifically, I experienced a `NoMethodError: undefined method
'backtrace'` when an error occurred within `pry`.
  • Loading branch information
harmonymjb authored and hakanensari committed Nov 4, 2024
1 parent 35a0ba1 commit 0d2578c
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/peddler/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class NotFound < Error; end
class QuotaExceeded < Error; end
class Unauthorized < Error; end

attr_reader :cause
attr_reader :response

# @!visibility private
class << self
Expand All @@ -23,8 +23,8 @@ def build(response)
end
end

def initialize(msg = nil, cause = nil)
@cause = cause
def initialize(msg = nil, response = nil)
@response = response
super(msg)
end
end
Expand Down

0 comments on commit 0d2578c

Please sign in to comment.