It would be convenient to perform pattern matching with exception classes. So Exception#deconstruct_keys should return a hash with :message (original_message) as well as any other keys specific to the exception subclass.
Examples:
begin
#code
rescue => err
case err
in StandardError(message: /Permission denied/)
abort "please select a different file"
in NameError(name: :foo)
retry if require "foo_helper
else
raise
end
end
+1 for making exceptions pattern-matching friendly. The example above demonstrates the use case pretty well (class + message matching).
The question is what keys must be supported for each standard exception class? The plain Ruby implementation could be as follows:
classException# Not sure if we need to take into account the `keys` argumentdefdeconstruct_keys(*)={message:,cause:}endclassNameErrordefdeconstruct_keys(*)=super.merge(name:)end
Even assuming some of those keys are not relevant, that's still a lot of #deconstruct_keys to define for various classes, so maybe a dynamic approach like below would be more feasible?