web-push icon indicating copy to clipboard operation
web-push copied to clipboard

Introduce UnknownError

Open twe4ked opened this issue 5 months ago • 4 comments

Hello!

Rather than raising ResponseError, which is also used as a base class for other errors, we now raise UnknownError.

This results in more specific error reporting when an unknown error occurs. For example, you can now handle unknown errors separately to ResponseError exceptions.

Compatibility:

Because UnknownError is still a subclass of ResponseError, if you are currently rescuing ResponseError or using Object#is_a?, your code will still work. If you're checking against the specific constant you will need to update your code.

Why?

If you're handling errors from the library by rescuing them like this:

begin
  WebPush.payload_send(…)
rescue WebPush::InvalidSubscription, WebPush::ExpiredSubscription
  # Handle invalid subscriptions in some way (we mark them as deleted)
rescue WebPush::ResponseError => e
  # Handle all known response errors in the same way.
  #
  # This will also catch "unknown errors" which aren't expected by the library.
rescue Errno::ECONNRESET, Net::OpenTimeout, OpenSSL::SSL::SSLError
  # Handle connection errors
end

Because the unknown error is just a WebPush::ResponseError it's more difficult to handle it differently without explicitly rescuing all the other kinds of WebPush::ResponseError earlier. With this change you can rescue WebPush::UnknownError before the generic WebPush::ResponseError and handle it separately. Unknown errors can be important to look at separately rather then rescuing them along with all the other known errors.

Cheers

twe4ked avatar Sep 17 '24 22:09 twe4ked