function internal_error
Flask error handler that catches internal server errors (HTTP 500), logs them, and returns a standardized JSON error response.
/tf/active/vicechatdev/docchat/app.py
1524 - 1526
simple
Purpose
This function serves as a centralized error handler for all unhandled exceptions in a Flask application. It is registered as an error handler for HTTP 500 errors using the @app.errorhandler(500) decorator. When any unhandled exception occurs during request processing, this function logs the error details and returns a user-friendly JSON response to the client, preventing sensitive error information from being exposed while maintaining proper error tracking through logging.
Source Code
def internal_error(error):
logger.error(f"Internal error: {error}")
return jsonify({'error': 'Internal server error'}), 500
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
error |
- | - | positional_or_keyword |
Parameter Details
error: The exception object or error information passed by Flask when an internal server error occurs. This can be any Exception instance or error details that caused the 500 error. Flask automatically passes this parameter when the error handler is invoked.
Return Value
Returns a tuple containing: (1) a JSON response object with a dictionary containing an 'error' key with the value 'Internal server error', and (2) the HTTP status code 500. The JSON response format is {'error': 'Internal server error'} which provides a generic error message to clients without exposing internal implementation details.
Dependencies
flasklogging
Required Imports
from flask import Flask
from flask import jsonify
import logging
Usage Example
from flask import Flask, jsonify
import logging
app = Flask(__name__)
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.ERROR)
@app.errorhandler(500)
def internal_error(error):
logger.error(f"Internal error: {error}")
return jsonify({'error': 'Internal server error'}), 500
@app.route('/test')
def test_route():
# This will trigger the error handler
raise Exception('Something went wrong')
if __name__ == '__main__':
app.run()
Best Practices
- Always configure logging before using this error handler to ensure errors are properly recorded
- This handler returns a generic error message to prevent exposing sensitive internal details to clients
- The error parameter should be logged but not returned directly to the client for security reasons
- Consider adding additional error context to logs (like request path, user ID, timestamp) for better debugging
- In production, ensure logger is configured to write to persistent storage (files, external logging services)
- This handler should be registered after the Flask app is created but before the app starts running
- Consider implementing different error handlers for different error types (404, 403, etc.) for better error management
- The generic error message can be customized based on environment (more details in development, generic in production)
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function internal_error_v2 93.2% similar
-
function internal_error_v1 90.1% similar
-
function handle_exception 87.2% similar
-
function not_found_v1 64.3% similar
-
function not_found 60.1% similar