This document explains HTTP status codes, error handling in Flask, and how to return appropriate error responses from API endpoints, including application-level error handlers.
This document covers HTTP status codes, error handling in Flask, and best practices for returning error responses from API endpoints. It explains status code categories, custom error responses, and application-level error handlers for robust API design.
Every HTTP response includes a three-digit status code that indicates the result of the request. Status codes are grouped into categories:
| Code Range | Category | Description |
|---|---|---|
| 100–199 | Informational | Request received, continuing process |
| 200–299 | Success | Request received and processed successfully |
| 300–399 | Redirection | Further action needed to complete request |
| 400–499 | Client Error | Error in the request from the client |
| 500–599 | Server Error | Error on the server side |
Common codes include:
| Code | Meaning |
|---|---|
| 200 | OK |
| 201 | Created |
| 202 | Accepted |
| 204 | No Content |
| 400 | Bad Request |
| 401 | Unauthorized |
| 403 | Forbidden |
| 404 | Not Found |
| 405 | Method Not Allowed |
| 422 | Unprocessable Entity |
| 500 | Internal Server Error |
Flask returns a 200 OK status by default when a route returns a response. To return a different status code, provide it as a tuple or use the make_response method.
1from flask import Flask, make_response
2
3app = Flask(__name__)
4
5@app.route('/')
6def home():
7 return '<h1>My First Application in Action</h1>', 200
8
9@app.route('/custom')
10def custom():
11 response = make_response('<h1>Custom Response</h1>', 200)
12 return response
When building APIs, return appropriate status codes for different outcomes:
1@app.route('/search')
2def search_response():
3 query = request.args.get('q')
4 if not query:
5 return {'message': 'Input parameter missing'}, 422
6 result = fetch_from_database(query)
7 if result:
8 return result, 200
9 else:
10 return {'message': 'Resource not found'}, 404
This example returns 422 if a required parameter is missing, 200 if the resource is found, and 404 if not found.
Flask allows defining custom error handlers for specific status codes at the application level:
1@app.errorhandler(404)
2def not_found(error):
3 return {'message': 'API not found'}, 404
4
5@app.errorhandler(500)
6def server_error(error):
7 return {'message': 'Something went wrong on the server'}, 500
These handlers return custom messages and status codes for 404 and 500 errors.
HTTP status codes communicate the result of API requests. Flask provides default and custom ways to return status codes and error messages, including application-level error handlers for robust API design.
(2) HTTP status codes indicate the result of the request, such as success, client error, or server error.
| Code | Meaning |
|---|---|
| A. 200 | 1. Unauthorized |
| B. 201 | 2. Created |
| C. 401 | 3. OK |
| D. 404 | 4. Not Found |
A-3, B-2, C-1, D-4.
(1) Flask does not always return a 200 status code; custom codes can be returned.
Flask allows defining application-level error handlers for specific status codes.
True. Flask supports custom error handlers for status codes like 404 and 500.