0

I have some decorators to handle specific errors from my application like AuthError or AppError.

Now I want to handle all other errors that are different from AuthError or AppError. Is possible to create a "generic" decorator that get other Exceptions?

For now, my code is:

@api.errorhandler(AuthError)
def handle_auth_error(ex):    
    logging.warn('An auth error has happened.', ex)
    return { 'message': ex.error}, ex.status_code

@api.errorhandler(AppError)
def handle_app_error(ex):    
    logging.error('An app error has happened.', ex)
    return { 'message': ex.error}, ex.status_code
  • `@api.errorhandler(Exception)`? – Wyrmwood Aug 27 '19 at 18:48
  • `@api.errorhandler(Exception)` will not start to handle `AppError` and `AuthError`? –  Aug 27 '19 at 18:49
  • If you want to handle all other errors using a custom handler, you can override the default handler using just `@api.errorhandler` instead. – joaovictortr Aug 27 '19 at 18:52
  • @joaovictortr just remove class from ()? woot, python is magic –  Aug 27 '19 at 18:53
  • 2
    Indeed, as the documentation states for the error handlers (https://flask-restplus.readthedocs.io/en/stable/errors.html#the-api-errorhandler-decorator). When flask-restplus can't find a proper handler, it uses the default one, which can be overriden by using `@api.errorhandler`, without any exceptions. – joaovictortr Aug 27 '19 at 18:55

0 Answers0