Skip to content

Commit

Permalink
better error handling and remove traceback logs
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelMWS committed Oct 16, 2024
1 parent 3006dca commit cbebf0c
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions prom2teams/app/versions/v2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from flask_restx import Api
from werkzeug.exceptions import HTTPException

log = logging.getLogger(__name__)

Expand All @@ -9,6 +10,26 @@

@api_v2.errorhandler
def default_error_handler(e):
# Define a default error message at the start
msg = 'An unhandled exception occurred.'
log.exception(msg + e)
return {'message': msg}, 500

# Log the full stack trace for debugging
# log.exception(f"{msg}: {str(e)}\n{traceback.format_exc()}")

# Customize the response based on the type of exception
if isinstance(e, HTTPException):
# Set a specific message for HTTPException
msg = e.description
# Return a dictionary for the HTTPException's description and code
return {'message': msg}, e.code
elif isinstance(e, ValueError):
# For ValueError, set a specific message and return a 400 Bad Request code
msg = 'Invalid value provided.'
return {'message': msg}, 400
elif isinstance(e, KeyError):
# For KeyError, set a specific message and return a 400 Bad Request code
msg = 'Missing required data.'
return {'message': msg}, 400
else:
# Generic response for other unhandled exceptions
return {'message': msg}, 500

0 comments on commit cbebf0c

Please sign in to comment.