29 lines
895 B
Python
29 lines
895 B
Python
import traceback
|
|
|
|
from flask.app import Flask
|
|
from werkzeug.exceptions import HTTPException
|
|
|
|
from ceo_common.errors import UserNotFoundError, GroupNotFoundError
|
|
from ceo_common.logger_factory import logger_factory
|
|
|
|
__all__ = ['register_error_handlers']
|
|
logger = logger_factory(__name__)
|
|
|
|
|
|
def register_error_handlers(app: Flask):
|
|
"""Register error handlers for the application."""
|
|
app.register_error_handler(Exception, generic_error_handler)
|
|
|
|
|
|
def generic_error_handler(err: Exception):
|
|
"""Return JSON for internal server errors."""
|
|
# pass through HTTP errors
|
|
if isinstance(err, HTTPException):
|
|
return err
|
|
if isinstance(err, UserNotFoundError) or isinstance(err, GroupNotFoundError):
|
|
status_code = 404
|
|
else:
|
|
status_code = 500
|
|
logger.error(traceback.format_exc())
|
|
return {'error': type(err).__name__ + ': ' + str(err)}, status_code
|