kaitai_struct_ruby_runtime icon indicating copy to clipboard operation
kaitai_struct_ruby_runtime copied to clipboard

Custom errors should inherit from `StandardError` (not `Exception`)

Open generalmimon opened this issue 2 years ago • 1 comments

See https://www.honeybadger.io/blog/ruby-exception-vs-standarderror-whats-the-difference/#custom-exceptions-should-inherit-from-standarderror:

It means you should always inherit from StandardError, and NEVER from Exception. Inheriting from Exception is bad because it breaks the expected behavior of rescue. People will think they're rescuing all application-level errors but yours will just sail on through.

https://github.com/kaitai-io/kaitai_struct_ruby_runtime/blob/ecf67519a990296bb1e095363ef2dc414e600502/lib/kaitai/struct/struct.rb#L688-L692

But UnexpectedDataError should be left as is, because it only remains for compatibility with 0.8 and older KS versions (and will be eventually removed):

https://github.com/kaitai-io/kaitai_struct_ruby_runtime/blob/ecf67519a990296bb1e095363ef2dc414e600502/lib/kaitai/struct/struct.rb#L78-L84

generalmimon avatar Feb 12 '23 09:02 generalmimon

Yeah, this issue is also reported by RuboCop:

lib/kaitai/struct/struct.rb:692:27: W: [Correctable] Lint/InheritException: Inherit from StandardError instead of Exception.
class KaitaiStructError < Exception
                          ^^^^^^^^^

We should adopt https://github.com/kaitai-io/kaitai_struct_visualizer/issues/38 for runtime library too, and make sure that RuboCop at least doesn't issue any warnings.

generalmimon avatar Feb 12 '23 09:02 generalmimon

Thanks for tracking and fixing this!

GreyCat avatar Apr 21 '25 13:04 GreyCat