Skip to content

exception_handlers

ASGI app exception handlers.

These are in addition to the exception handlers available in OPTIMADE Python tools. For more information see https://www.optimade.org/optimade-python-tools/api_reference/server/exception_handlers/.

request_validation_exception_handler(request, exc) async

Special handler if a RequestValidationError comes from wrong POST data

Source code in optimade_gateway/exception_handlers.py
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
async def request_validation_exception_handler(
    request: Request, exc: RequestValidationError
) -> JSONResponse:
    """Special handler if a `RequestValidationError` comes from wrong `POST` data"""
    status_code = 500
    if request.method in ("POST", "post"):
        status_code = 400

    errors = set()
    for error in exc.errors():
        pointer = "/" + "/".join([str(_) for _ in error["loc"]])
        source = ErrorSource(pointer=pointer)
        code = error["type"]
        detail = error["msg"]
        errors.add(
            OptimadeError(
                detail=detail,
                status=status_code,
                title=str(exc.__class__.__name__),
                source=source,
                code=code,
            )
        )

    return general_exception(request, exc, status_code=status_code, errors=list(errors))