From 0d2578c7b8bf8ce4928cf915b496c767a10c5b24 Mon Sep 17 00:00:00 2001 From: Harmony Bouvier Date: Mon, 4 Nov 2024 13:01:09 -0800 Subject: [PATCH] Set the failed http response in a custom attribute instead of cause MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `#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`. --- lib/peddler/error.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/peddler/error.rb b/lib/peddler/error.rb index c9ddc7e4..8500d8fc 100644 --- a/lib/peddler/error.rb +++ b/lib/peddler/error.rb @@ -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 @@ -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