How to use Foreman exceptions

Hello,

friendly reminder for Foreman devs (and new info for Katello plugin
devs) follows.

We have defined two exceptions Foreman::Exception and
Foreman::WrappedException. Both are generic exceptions which 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(N_("Localized error message"), wrapped_exception)

It is very important to use N_ and not the simple _ (underscore)
function and to avoid using Ruby string interpolation, because those
exceptions prints out error codes 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]].

Feel free to use Foreman exceptions in plugins as well. Good example is
Foreman Discovery plugin.

Links:

http://projects.theforeman.org/projects/foreman/wiki/Translating#Exceptions
http://projects.theforeman.org/projects/foreman/wiki/ErrorCodes

··· -- Later,

Lukas “lzap” Zapletal
irc: lzap #theforeman