Dmitry Afanasyev bf7a5520dc
add api exception handler (#74)
* try to add exception handler

* improve server error test

* fix lint

* add build_uri util

* fix header file path

---------

Co-authored-by: Dmitry Afanasyev <afanasiev@litres.ru>
2023-12-30 23:50:59 +03:00

28 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from typing import Any
from fastapi import status as status_code
from pydantic import BaseModel, Field
class BaseError(BaseModel):
"""Error response as defined in
https://datatracker.ietf.org/doc/html/rfc7807.
One difference is that the member "type" is not a URI
"""
type: str | None = Field(title="The name of the class of the error")
title: str | None = Field(
title="A short, human-readable summary of the problem that does not change from occurence to occurence",
)
detail: str | None = Field(
default=None, title="А human-readable explanation specific to this occurrence of the problem"
)
instance: Any | None = Field(default=None, title="Identifier for this specific occurrence of the problem")
class BaseResponse(BaseModel):
status: int = Field(..., title="Status code of request.", example=status_code.HTTP_200_OK) # type: ignore[call-arg]
error: dict[Any, Any] | BaseError | None = Field(default=None, title="Errors")
payload: Any | None = Field(default_factory=dict, title="Payload data.")