exception_handlers¶
CONFIG: ServerConfig = ServerConfig()
module-attribute
¶
This singleton loads the config from a hierarchy of sources (see
customise_sources
)
and makes it importable in the server code.
LOGGER = logging.getLogger('optimade')
module-attribute
¶
OPTIMADE_EXCEPTIONS: Iterable[tuple[type[Exception], Callable[[Request, Exception], JSONAPIResponse]]] = [(StarletteHTTPException, http_exception_handler), (OptimadeHTTPException, http_exception_handler), (RequestValidationError, request_validation_exception_handler), (ValidationError, validation_exception_handler), (VisitError, grammar_not_implemented_handler), (NotImplementedError, not_implemented_handler), (Exception, general_exception_handler)]
module-attribute
¶
A tuple of all pairs of exceptions and handler functions that allow for appropriate responses to be returned in certain scenarios according to the OPTIMADE specification.
To use these in FastAPI app code:
from fastapi import FastAPI
app = FastAPI()
for exception, handler in OPTIMADE_EXCEPTIONS:
app.add_exception_handler(exception, handler)
BadRequest
¶
Bases: OptimadeHTTPException
400 Bad Request
Source code in optimade/exceptions.py
57 58 59 60 61 |
|
ErrorResponse
¶
Bases: Response
errors MUST be present and data MUST be skipped
Source code in optimade/models/responses.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
model_config = ConfigDict(json_encoders={datetime: lambda : v.astimezone(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')})
class-attribute
instance-attribute
¶
The specification mandates that datetimes must be encoded following RFC3339, which does not support fractional seconds, thus they must be stripped in the response. This can cause issues when the underlying database contains fields that do include microseconds, as filters may return unexpected results.
ErrorSource
¶
Bases: BaseModel
an object containing references to the source of the error
Source code in optimade/models/jsonapi.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
|
JSONAPIResponse
¶
Bases: JSONResponse
This class simply patches fastapi.responses.JSONResponse
to use the
JSON:API 'application/vnd.api+json' MIME type.
Source code in optimade/server/routers/utils.py
41 42 43 44 45 46 47 |
|
OptimadeError
¶
Bases: Error
detail MUST be present
Source code in optimade/models/optimade_json.py
126 127 128 129 130 131 132 133 134 |
|
OptimadeHTTPException
¶
Bases: Exception
, ABC
This abstract class can be subclassed to define HTTP responses with the desired status codes, and detailed error strings to represent in the JSON:API error response.
This class closely follows the starlette.HTTPException
without
requiring it as a dependency, so that such errors can also be
raised from within client code.
Attributes:
Name | Type | Description |
---|---|---|
status_code |
int
|
The HTTP status code accompanying this exception. |
title |
str
|
A descriptive title for this exception. |
detail |
Optional[str]
|
An optional string containing the details of the error. |
Source code in optimade/exceptions.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
general_exception(request, exc, status_code=500, errors=None)
¶
Handle an exception
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request |
Request
|
The HTTP request resulting in the exception being raised. |
required |
exc |
Exception
|
The exception being raised. |
required |
status_code |
int
|
The returned HTTP status code for the error response. |
500
|
errors |
Optional[list[OptimadeError]]
|
List of error resources as defined in the OPTIMADE specification. |
None
|
Returns:
Type | Description |
---|---|
JSONAPIResponse
|
A JSON HTTP response based on |
Source code in optimade/server/exception_handlers.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
general_exception_handler(request, exc)
¶
Catch all Python Exceptions not handled by other exception handlers
Pass-through directly to general_exception()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request |
Request
|
The HTTP request resulting in the exception being raised. |
required |
exc |
Exception
|
The exception being raised. |
required |
Returns:
Type | Description |
---|---|
JSONAPIResponse
|
A JSON HTTP response through |
Source code in optimade/server/exception_handlers.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
|
grammar_not_implemented_handler(request, exc)
¶
Handle an error raised by Lark during filter transformation
All errors raised during filter transformation are wrapped in the Lark VisitError
.
According to the OPTIMADE specification, these errors are repurposed to be 501 NotImplementedErrors.
For special exceptions, like BadRequest
, we pass-through to
general_exception()
, since they should not
return a 501 NotImplementedError.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request |
Request
|
The HTTP request resulting in the exception being raised. |
required |
exc |
VisitError
|
The exception being raised. |
required |
Returns:
Type | Description |
---|---|
JSONAPIResponse
|
A JSON HTTP response through |
Source code in optimade/server/exception_handlers.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
|
http_exception_handler(request, exc)
¶
Handle a general HTTP Exception from Starlette
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request |
Request
|
The HTTP request resulting in the exception being raised. |
required |
exc |
Union[StarletteHTTPException, OptimadeHTTPException]
|
The exception being raised. |
required |
Returns:
Type | Description |
---|---|
JSONAPIResponse
|
A JSON HTTP response through |
Source code in optimade/server/exception_handlers.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
|
meta_values(url, data_returned, data_available, more_data_available, schema=None, **kwargs)
¶
Helper to initialize the meta values
Source code in optimade/server/routers/utils.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
not_implemented_handler(request, exc)
¶
Handle a standard NotImplementedError Python exception
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request |
Request
|
The HTTP request resulting in the exception being raised. |
required |
exc |
NotImplementedError
|
The exception being raised. |
required |
Returns:
Type | Description |
---|---|
JSONAPIResponse
|
A JSON HTTP response through |
Source code in optimade/server/exception_handlers.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
request_validation_exception_handler(request, exc)
¶
Handle a request validation error from FastAPI
RequestValidationError
is a specialization of a general pydantic ValidationError
.
Pass-through directly to general_exception()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request |
Request
|
The HTTP request resulting in the exception being raised. |
required |
exc |
RequestValidationError
|
The exception being raised. |
required |
Returns:
Type | Description |
---|---|
JSONAPIResponse
|
A JSON HTTP response through |
Source code in optimade/server/exception_handlers.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
|
validation_exception_handler(request, exc)
¶
Handle a general pydantic validation error
The pydantic ValidationError
usually contains a list of errors,
this function extracts them and wraps them in the OPTIMADE specific error resource.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request |
Request
|
The HTTP request resulting in the exception being raised. |
required |
exc |
ValidationError
|
The exception being raised. |
required |
Returns:
Type | Description |
---|---|
JSONAPIResponse
|
A JSON HTTP response through |
Source code in optimade/server/exception_handlers.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
|