🔍 Code Extractor

function not_found_v2

Maturity: 22

Flask error handler that returns a JSON response with a 404 status code when a resource is not found.

File:
/tf/active/vicechatdev/vice_ai/new_app.py
Lines:
383 - 384
Complexity:
simple

Purpose

This function serves as a custom 404 error handler for a Flask web application. It intercepts all 404 Not Found errors and returns a standardized JSON response instead of the default HTML error page. This is particularly useful for REST APIs where consistent JSON responses are expected across all endpoints, including error cases.

Source Code

def not_found(error):
    return jsonify({'error': 'Resource not found'}), 404

Parameters

Name Type Default Kind
error - - positional_or_keyword

Parameter Details

error: The error object automatically passed by Flask's error handling mechanism. Contains information about the 404 error that was raised. This parameter is required by Flask's errorhandler decorator but is not used in the function body.

Return Value

Returns a tuple containing: (1) a JSON object with structure {'error': 'Resource not found'} created by Flask's jsonify function, and (2) the HTTP status code 404. Flask automatically converts this tuple into a proper HTTP response with JSON content-type headers.

Dependencies

  • flask

Required Imports

from flask import Flask
from flask import jsonify

Usage Example

from flask import Flask, jsonify

app = Flask(__name__)

@app.errorhandler(404)
def not_found(error):
    return jsonify({'error': 'Resource not found'}), 404

@app.route('/existing')
def existing_route():
    return jsonify({'message': 'This exists'})

if __name__ == '__main__':
    app.run()

# When accessing a non-existent route like '/nonexistent',
# the response will be: {'error': 'Resource not found'} with status 404

Best Practices

  • Always use the @app.errorhandler(404) decorator to register this function with Flask
  • Ensure consistent error response format across all error handlers in your API
  • Consider adding additional error details like timestamp or request path for better debugging
  • This handler will catch all 404 errors including missing static files - ensure this is desired behavior
  • For production APIs, consider logging the error parameter for monitoring and debugging purposes
  • Can be extended to include request.path or request.url to inform clients which resource was not found

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function not_found_v1 94.1% similar

    Flask error handler that returns a JSON response with a 404 status code when a requested resource is not found.

    From: /tf/active/vicechatdev/docchat/app.py
  • function not_found 82.0% similar

    Flask error handler function that renders a custom error page when a 404 (Page Not Found) error occurs.

    From: /tf/active/vicechatdev/vice_ai/app.py
  • function not_found_error 82.0% similar

    Flask error handler that renders a custom error page when a 404 (Page Not Found) error occurs.

    From: /tf/active/vicechatdev/full_smartstat/app.py
  • function handle_exception 61.7% similar

    Flask error handler that catches all unhandled exceptions, logs them, and returns a generic JSON error response with HTTP 500 status.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function internal_error 58.1% similar

    Flask error handler that catches internal server errors (HTTP 500), logs them, and returns a standardized JSON error response.

    From: /tf/active/vicechatdev/docchat/app.py
← Back to Browse