Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
tracer = trace.get_tracer(__name__)
# ------------------------------------------------------------------

APP = FastAPI(**config.API_CONFIG)
APP = FastAPI(servers=[{"url": config.API_URL_ROOT}], **config.API_CONFIG)

if config.OPENTELEMETRY_ENABLED:
FastAPIInstrumentor.instrument_app(APP)
Expand Down
65 changes: 30 additions & 35 deletions app/routers/error_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,29 @@

import logging
from urllib.parse import urlsplit, urlunsplit, quote

from typing import Optional, List
from pydantic import BaseModel, constr

from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse
from fastapi.exceptions import RequestValidationError
from starlette.exceptions import HTTPException as StarletteHTTPException

class InvalidParam(BaseModel):
name: str
reason: str

class Problem(BaseModel):
type: constr(min_length=1)
title: Optional[str] = None
status: int
detail: Optional[str] = None
instance: Optional[str] = None
invalid_params: Optional[List[InvalidParam]] = None

class Config:
extra = "allow"

def get_url_base(request: Request) -> str:
"""Return the base URL for the API."""
Expand Down Expand Up @@ -158,6 +176,7 @@ async def starlette_handler(request: Request, exc: StarletteHTTPException):
err_msg = ""
if hasattr(exc, "detail") and exc.detail:
err_msg = exc.detail

if exc.status_code == 404:
return problem_response(
request=request,
Expand Down Expand Up @@ -197,30 +216,6 @@ async def global_handler(request: Request, exc: Exception):
problem_type="internal-error",
)


DEFAULT_PROBLEM_SCHEMA = {
"type": "object",
"properties": {
"type": {"type": "string"},
"title": {"type": "string"},
"status": {"type": "integer"},
"detail": {"type": "string"},
"instance": {"type": "string"},
"invalid_params": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"reason": {"type": "string"},
},
"required": ["name", "reason"],
},
},
},
"required": ["type", "title", "status", "detail", "instance"],
}

EXAMPLE_400 = {
"type": "https://iri.example.com/problems/invalid-parameter",
"title": "Invalid parameter",
Expand Down Expand Up @@ -308,15 +303,16 @@ async def global_handler(request: Request, exc: Exception):
DEFAULT_RESPONSES = {
400: {
"description": "Invalid request parameters",
"model": Problem,
"content": {
"application/problem+json": {
"schema": DEFAULT_PROBLEM_SCHEMA,
"example": EXAMPLE_400,
}
},
},
401: {
"description": "Unauthorized",
"model": Problem,
"headers": {
"WWW-Authenticate": {
"description": "Bearer authentication challenge",
Expand All @@ -325,31 +321,31 @@ async def global_handler(request: Request, exc: Exception):
},
"content": {
"application/problem+json": {
"schema": DEFAULT_PROBLEM_SCHEMA,
"example": EXAMPLE_401,
}
},
},
403: {
"description": "Forbidden",
"model": Problem,
"content": {
"application/problem+json": {
"schema": DEFAULT_PROBLEM_SCHEMA,
"example": EXAMPLE_403,
}
},
},
404: {
"description": "Not Found",
"model": Problem,
"content": {
"application/problem+json": {
"schema": DEFAULT_PROBLEM_SCHEMA,
"example": EXAMPLE_404,
}
},
},
405: {
"description": "Method Not Allowed",
"model": Problem,
"headers": {
"Allow": {
"description": "Allowed HTTP methods",
Expand All @@ -358,61 +354,60 @@ async def global_handler(request: Request, exc: Exception):
},
"content": {
"application/problem+json": {
"schema": DEFAULT_PROBLEM_SCHEMA,
"example": EXAMPLE_405,
}
},
},
409: {
"description": "Conflict",
"model": Problem,
"content": {
"application/problem+json": {
"schema": DEFAULT_PROBLEM_SCHEMA,
"example": EXAMPLE_409,
}
},
},
422: {
"description": "Unprocessable Entity",
"model": Problem,
"content": {
"application/problem+json": {
"schema": DEFAULT_PROBLEM_SCHEMA,
"example": EXAMPLE_422,
}
},
},
500: {
"description": "Internal Server Error",
"model": Problem,
"content": {
"application/problem+json": {
"schema": DEFAULT_PROBLEM_SCHEMA,
"example": EXAMPLE_500,
}
},
},
501: {
"description": "Not Implemented",
"model": Problem,
"content": {
"application/problem+json": {
"schema": DEFAULT_PROBLEM_SCHEMA,
"example": EXAMPLE_501,
}
},
},
503: {
"description": "Service Unavailable",
"model": Problem,
"content": {
"application/problem+json": {
"schema": DEFAULT_PROBLEM_SCHEMA,
"example": EXAMPLE_503,
}
},
},
504: {
"description": "Gateway Timeout",
"model": Problem,
"content": {
"application/problem+json": {
"schema": DEFAULT_PROBLEM_SCHEMA,
"example": EXAMPLE_504,
}
},
Expand Down