kaitai_struct_ruby_runtime
kaitai_struct_ruby_runtime copied to clipboard
Custom errors should inherit from `StandardError` (not `Exception`)
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 fromException. 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
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.
Thanks for tracking and fixing this!