diff --git a/r2/r2/lib/errors.py b/r2/r2/lib/errors.py index d998424b0..b414ebaf3 100644 --- a/r2/r2/lib/errors.py +++ b/r2/r2/lib/errors.py @@ -126,6 +126,18 @@ error_list = dict(( errors = Storage([(e, e) for e in error_list.keys()]) +def add_error_codes(new_codes): + """Add error codes to the error enumeration. + + It is assumed that the incoming messages are marked for translation but not + yet translated, so they can be declared before pylons.i18n is ready. + + """ + for code, message in new_codes.iteritems(): + error_list[code] = _(message) + errors[code] = code + + class RedditError(Exception): name = None fields = None diff --git a/r2/r2/lib/plugin.py b/r2/r2/lib/plugin.py index fa6648575..78e50578a 100644 --- a/r2/r2/lib/plugin.py +++ b/r2/r2/lib/plugin.py @@ -33,6 +33,7 @@ class Plugin(object): config = {} live_config = {} needs_static_build = False + errors = {} def __init__(self, entry_point): self.entry_point = entry_point @@ -145,5 +146,10 @@ class PluginLoader(object): plugin.on_load(g) def load_controllers(self): + # this module relies on pylons.i18n._ at import time (for translating + # messages) which isn't available 'til we're in request context. + from r2.lib import errors + for plugin in self: + errors.add_error_codes(plugin.errors) plugin.load_controllers()