WeblateOrg / weblate

Web based localization tool with tight version control integration.
https://weblate.org/
GNU General Public License v3.0
4.67k stars 1.02k forks source link

Validate lazily loaded classes in deploy checks #12135

Closed dleidert closed 1 month ago

dleidert commented 4 months ago

Describe the issue

I run weblate in docker with an Apache reverse proxy setup. The docker-compose configuration contains a setting for WEBLATE_ADD_ADDONS. Now, I recently discovered that there was a "typo" in one of the add-ons listed there: weblate.addons.consistency.LangaugeConsistencyAddon. I then though, that I might have caused this typo myself, "fixed" it, and restarted the instance. Soon after, I started to observe internal server errors that were caught by Sentry. The Sentry backtrace shows this:

ImproperlyConfigured
Module 'weblate.addons.consistency' does not define a 'LanguageConsistencyAddon' class in WEBLATE_ADDONS

weblate/utils/classloader.py in load_class at line 29
[..]

Now, I do understand the error, and I changed the add-on name back to what seems to be the correct name. But shouldn't weblate handle this situation more gracefully than causing an internal server error? In my case, the error was thrown while I was creating a new component, which lead to empty translations in that component.

I already tried

Steps to reproduce the behavior

  1. Create a docker-compose override file which contains e.g. WEBLATE_ADD_ADDONS=weblate.addons.consistency.LanguageConsistencyAddon.
  2. Start the instance
  3. Create a project and try to access the add-ons
  4. The instance fails with an internal server error

Expected behavior

Handle the situation more gracefully without causing an internal server error.

Screenshots

No response

Exception traceback

weblate/utils/classloader.py in load_class at line 29:

            f"Error importing module {module!r} in {setting}: {error}"
        ) from error
    try:
        return getattr(mod, attr)
    except AttributeError as error:
        raise ImproperlyConfigured(
              ^^^^^^^^^^^^^^^^^^^^
            f"Module {module!r} does not define a {attr!r} class in {setting}"
        ) from error
class ClassLoader:

How do you run Weblate?

Docker container

Weblate versions

Weblate deploy checks

System check identified no issues (1 silenced).

Additional context

No response

nijel commented 4 months ago

The classes are currently loaded on demand to avoid loading them when not needed. This will lead to raising ImproperlyConfigured errors at runtime.

Checking the validity of all ClassLoader instances should be probably done as deploy check so that it is visible while running weblate check --deploy and in the management interface.

github-actions[bot] commented 4 months ago

This issue seems to be a good fit for newbie contributors. You are welcome to contribute to Weblate! Don't hesitate to ask any questions you would have while implementing this.

You can learn about how to get started in our contributors documentation.

github-actions[bot] commented 1 month ago

Thank you for your report; the issue you have reported has just been fixed.

github-actions[bot] commented 1 month ago

Thank you for your report; the issue you have reported has just been fixed.