Home > Backend Development > Python Tutorial > How to Handle Custom 404 Not Found Responses in FastAPI?

How to Handle Custom 404 Not Found Responses in FastAPI?

DDD
Release: 2024-10-24 02:06:29
Original
974 people have browsed it

How to Handle Custom 404 Not Found Responses in FastAPI?

How to Return a Custom 404 Not Found Page Using FastAPI?

When you encounter a 404 response status code, a default error page is usually displayed in web applications. However, you may want to provide a custom 404 page for a more personalized experience. Using FastAPI, implementing a custom 404 page is straightforward.

Custom Exception Handler

An effective method is to leverage a custom exception handler. By specifying the status code you want to handle, you can create a targeted handler.

<code class="python">from fastapi.responses import RedirectResponse
from fastapi.exceptions import HTTPException

@app.exception_handler(404)
async def not_found_exception_handler(request: Request, exc: HTTPException):
    return RedirectResponse('https://example.com/404')</code>
Copy after login

Exception Handlers Parameter

Alternatively, you can use the exception_handlers parameter of the FastAPI class.

<code class="python">from fastapi import FastAPI, Request

def not_found_error(request: Request, exc: HTTPException):
    return RedirectResponse('https://example.com/404')

exception_handlers = {404: not_found_error}
app = FastAPI(exception_handlers=exception_handlers)</code>
Copy after login

Note: In these examples, a redirect is performed. However, you can return any custom Response, HTMLResponse, or Jinja2 TemplateResponse as needed.

Working Example

Consider the following example:

app.py

<code class="python">from fastapi.responses import RedirectResponse
from fastapi.exceptions import HTTPException
from fastapi import Request

async def not_found_error(request: Request, exc: HTTPException):
    return RedirectResponse('https://example.com/404')

async def internal_error(request: Request, exc: HTTPException):
    return RedirectResponse('https://example.com/500')

exception_handlers = {404: not_found_error, 500: internal_error}
app = FastAPI(exception_handlers=exception_handlers)</code>
Copy after login

404.html Template

<code class="html"><!DOCTYPE html>
<html>
<title>404 Not Found</title>
<body>
<h1>Not Found</h1>
<p>The requested resource could not be found.</p>
</body>
</html></code>
Copy after login

500.html Template

<code class="html"><!DOCTYPE html>
<html>
<title>500 Internal Server Error</title>
<body>
<h1>Internal Server Error</h1>
<p>An internal error has occurred. Please try again later.</p>
</body>
</html></code>
Copy after login

The above is the detailed content of How to Handle Custom 404 Not Found Responses in FastAPI?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template