Skip to content

Commit

Permalink
Provide clearer error messages when app fails to load.
Browse files Browse the repository at this point in the history
  • Loading branch information
thedrow authored and auvipy committed Oct 1, 2020
1 parent 86e0d93 commit 8767df0
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions celery/bin/celery.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Celery Command Line Interface."""
import os
import traceback

import click
import click.exceptions
Expand All @@ -25,6 +26,19 @@
from celery.bin.upgrade import upgrade
from celery.bin.worker import worker

UNABLE_TO_LOAD_APP_MODULE_NOT_FOUND = click.style("""
Unable to load celery application.
The module {0} was not found.""", fg='red')

UNABLE_TO_LOAD_APP_ERROR_OCCURRED = click.style("""
Unable to load celery application.
While trying to load the module {0} the following error occurred:
{1}""", fg='red')

UNABLE_TO_LOAD_APP_APP_MISSING = click.style("""
Unable to load celery application.
{0}""")


class App(ParamType):
"""Application option."""
Expand All @@ -34,8 +48,21 @@ class App(ParamType):
def convert(self, value, param, ctx):
try:
return find_app(value)
except (ModuleNotFoundError, AttributeError) as e:
self.fail(str(e))
except ModuleNotFoundError as e:
if e.name != value:
exc = traceback.format_exc()
self.fail(
UNABLE_TO_LOAD_APP_ERROR_OCCURRED.format(value, exc)
)
self.fail(UNABLE_TO_LOAD_APP_MODULE_NOT_FOUND.format(e.name))
except AttributeError as e:
attribute_name = e.args[0].capitalize()
self.fail(UNABLE_TO_LOAD_APP_APP_MISSING.format(attribute_name))
except Exception:
exc = traceback.format_exc()
self.fail(
UNABLE_TO_LOAD_APP_ERROR_OCCURRED.format(value, exc)
)


APP = App()
Expand Down

0 comments on commit 8767df0

Please sign in to comment.