Hey,
there are two “magic” exceptions in the Foreman codebase: Foreman::Exception
and Foreman::WrappedException
. Both are generic exceptions with “ERF” error code generation embedded and both are i18n-friendly. Use the following style to properly format messages:
raise ::Foreman::Exception.new(N_("Localized error message"))
raise ::Foreman::Exception.new(N_("Localized error message: %s", substring_message))
raise ::Foreman::Exception.new(N_("Localized error message %{a} and %{b}", {:a => a, :b => b}))
raise ::Foreman::WrappedException.new(wrapped_exception, N_("Localized error message"))
Note this is incorrect (percent vs comma interpolation):
raise ::Foreman::Exception.new(N_("Localized error message: %s") % substring_message)
It is important to use N_
and not the simple _
(underscore) function and to avoid using Ruby string interpolation, because those exceptions prints out error code which are generated from exception class names and main (untranslated) messages. There is also a rake task that goes through our codebase and generates list of all possible error codes which we keep on the wiki page ErrorCodes.
https://projects.theforeman.org/projects/foreman/wiki/ErrorCodes
If an exception is needed to be untranslated, it can be used without underscore functions. Beware, that parameters can be also passed, but Ruby only support hash interpolation:
raise ::Foreman::Exception.new("This works fine")
raise ::Foreman::Exception.new("This works fine %{foo}", {:foo => "too"})
raise ::Foreman::Exception.new("But this %{s} will not work", 'example')
TL;DR - To get your error code right, interpolate correctly.
https://projects.theforeman.org/projects/foreman/wiki/ErrorCodes
Maybe we could write a Hound rule and run this via github.com. We would probably find couple of other things