{"id":15103559,"url":"https://github.com/explosion/fastapi-explosion-extras","last_synced_at":"2026-01-12T13:59:53.981Z","repository":{"id":39348771,"uuid":"443174723","full_name":"explosion/fastapi-explosion-extras","owner":"explosion","description":null,"archived":false,"fork":false,"pushed_at":"2022-08-11T17:10:01.000Z","size":62,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-03-11T03:52:08.557Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/explosion.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-12-30T20:09:13.000Z","updated_at":"2022-01-07T20:43:28.000Z","dependencies_parsed_at":"2022-08-30T21:11:23.218Z","dependency_job_id":null,"html_url":"https://github.com/explosion/fastapi-explosion-extras","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/explosion%2Ffastapi-explosion-extras","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/explosion%2Ffastapi-explosion-extras/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/explosion%2Ffastapi-explosion-extras/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/explosion%2Ffastapi-explosion-extras/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/explosion","download_url":"https://codeload.github.com/explosion/fastapi-explosion-extras/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247093290,"owners_count":20882387,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-25T19:40:29.281Z","updated_at":"2026-01-12T13:59:53.941Z","avatar_url":"https://github.com/explosion.png","language":"Python","readme":"# FastAPI Extras\n\nThis library is a collection of utilities for running FastAPI applications at Explosion AI.\n\n\n## HttpizeErrorsAPIRouter\nThis custom router's main functionality is to handle errors per route instead of through a global exception handler by adding the `httpize_errors` keyword argument to the FastAPI route declaration. This allows each route to return normal informative Python errors instead of the FastAPI `HTTPException` class to get valid responses.\n\nIt also times each request and sets the `X-Response-Time` header on the Response\n\n**An example route**\n\n```python\n@router.get(\"/testing\", httpize_errors={ValueError: 400})\ndef test_route(i: int):\n    if i \u003c 1:\n        raise ValueError(\"Bad Input Data\")\n    return {\"i\": i}\n```\n\nIf the ValueError is raised, this custom router knows to return a Response with a status code of 400 (Bad Request) and the message provided to the `ValueError` \n\n\n**Usage**\n\nFastAPI doesn't have built-in support for overriding the app Router, however this is required since we add a new keyword argument to the route declaration. FastAPI doesn't pass `**kwargs` forward, it only passes explict named keyword arguments. \n\nTo get around this, we need to overwrite the app router manually and refresh the routes after all of them have been included in the main app. This looks like:\n\n```python\nfrom fastapi import FastAPI\nfrom fastapi_extras import HttpizeErrorsAPIRouter, init_app\nimport uvicorn\n\n\n# API Router (could be in another module)\napi_router = HttpizeErrorsAPIRouter(tags=[\"tests\"])\n\n\n@api_router.get(\"/testing\", httpize_errors={ValueError: 400})\ndef test_route(i: int):\n    if i \u003c 1:\n        raise ValueError(\"Bad Input Data\")\n    return {\"i\": i}\n\n\n# Main app definition\napp = FastAPI()\n\n# Overwrite App Router to use the custom HttpizeErrorsAPIRouter\napp.router = HttpizeErrorsAPIRouter.from_app(app)\n\n# Include API Router from above\napp.include_router(api_router)\n\n# Refresh the App (this rebuilds the Starlette Middleware Stack)\ninit_app(app)\n\nuvicorn.run(app)\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexplosion%2Ffastapi-explosion-extras","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fexplosion%2Ffastapi-explosion-extras","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexplosion%2Ffastapi-explosion-extras/lists"}