Hello,
during extracting of strings for translation, I stumbled upon
pure-string Ruby exceptions which are problematic when dealing with
localized strings.
Therefore two exceptions were created: ForemanException and
WrappedException (subclass). These are lightweight Ruby exceptions that
are i18n friendly and there is room for creating more subclasses for
various parts of Foreman.
https://github.com/lzap/foreman/blob/gettext-rebased2/lib/foreman/exception.rb
https://github.com/lzap/foreman/blob/gettext-rebased2/lib/foreman/wrapped_exception.rb
The exception has two unique features - it is i18n friendly, which means
it does gettext translation when gettext method (_) is found and second
- it provides error codes.
These error codes are hashes of class name (first part), and
untranslated message (second part). I do not use Ruby hash function of
string, because this can change across Ruby versions and implementation.
I use CRC32 for that.
The error codes are in this format: ERFXX-YYYY where XX are two decimals
(classname hash) and YYYY are four decimals (message hash). The complete
list of error codes is here:
http://projects.theforeman.org/projects/foreman/wiki/ErrorCodes
The idea is to put this into documentation and since these exceptions
provides these, users are able to google these finding more information
about them.
There is a simple rake task that generates the list.
https://github.com/lzap/foreman/blob/gettext-rebased2/lib/tasks/exception.rake
My motivation behind using "ERF" prefix was obvious - ERR for error and
F for foreman. Other projects like Katello or Signo can use the similar
pattern (ERK, ERS…) so we can distinguish among them easily.
And for the codes themselves - ability to have various "groups" is
handy, we just need to create our own exceptions like:
class OAuthException < ForemanException; end
And that's it.
I would like to see your opinions about the format of error codes and
overall.
Thanks
···
-- Later,Lukas “lzap” Zapletal
irc: lzap #theforeman