Coverage for src/zapy/api/exceptions.py: 79%
18 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-10 19:35 +0000
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-10 19:35 +0000
1import traceback
3from fastapi import Request
4from fastapi.responses import JSONResponse
5from pydantic import ValidationError
7from zapy.base.exceptions import HandledError, ZapyError
10def global_error_handler(_: Request, exception: Exception) -> JSONResponse:
11 traceback.print_exc()
12 status_code = getattr(exception, "status_code", 500)
13 error_type = getattr(exception, "error_type", "error:unhandled")
14 if isinstance(exception, HandledError): 14 ↛ 16line 14 didn't jump to line 16, because the condition on line 14 was never false
15 status_code = 400
16 if isinstance(exception, ValidationError): 16 ↛ 17line 16 didn't jump to line 17, because the condition on line 16 was never true
17 status_code = 400
18 error_type = "validation:json"
19 response = {
20 "error": str(exception),
21 "class": exception.__class__.__name__,
22 "error_type": error_type,
23 }
24 if isinstance(exception, ZapyError): 24 ↛ 26line 24 didn't jump to line 26, because the condition on line 24 was never false
25 response.update({"class": "ZapyError", "context": exception.context})
26 return JSONResponse(response, status_code=status_code)