Coverage for gws-app/gws/base/web/error.py: 89%
53 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-05 13:35 +0200
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-05 13:35 +0200
1"""HTTP exceptions and conversion of GWS errors to HTTP errors."""
3from typing import Optional
5import gws
6import werkzeug.exceptions
8BadRequest = werkzeug.exceptions.BadRequest
9Unauthorized = werkzeug.exceptions.Unauthorized
10Forbidden = werkzeug.exceptions.Forbidden
11NotFound = werkzeug.exceptions.NotFound
12MethodNotAllowed = werkzeug.exceptions.MethodNotAllowed
13NotAcceptable = werkzeug.exceptions.NotAcceptable
14RequestTimeout = werkzeug.exceptions.RequestTimeout
15Conflict = werkzeug.exceptions.Conflict
16Gone = werkzeug.exceptions.Gone
17LengthRequired = werkzeug.exceptions.LengthRequired
18PreconditionFailed = werkzeug.exceptions.PreconditionFailed
19RequestEntityTooLarge = werkzeug.exceptions.RequestEntityTooLarge
20RequestURITooLarge = werkzeug.exceptions.RequestURITooLarge
21UnsupportedMediaType = werkzeug.exceptions.UnsupportedMediaType
22RequestedRangeNotSatisfiable = werkzeug.exceptions.RequestedRangeNotSatisfiable
23ExpectationFailed = werkzeug.exceptions.ExpectationFailed
24ImATeapot = werkzeug.exceptions.ImATeapot
25UnprocessableEntity = werkzeug.exceptions.UnprocessableEntity
26Locked = werkzeug.exceptions.Locked
27PreconditionRequired = werkzeug.exceptions.PreconditionRequired
28TooManyRequests = werkzeug.exceptions.TooManyRequests
29RequestHeaderFieldsTooLarge = werkzeug.exceptions.RequestHeaderFieldsTooLarge
30UnavailableForLegalReasons = werkzeug.exceptions.UnavailableForLegalReasons
31InternalServerError = werkzeug.exceptions.InternalServerError
32NotImplemented = werkzeug.exceptions.NotImplemented
33BadGateway = werkzeug.exceptions.BadGateway
34ServiceUnavailable = werkzeug.exceptions.ServiceUnavailable
35GatewayTimeout = werkzeug.exceptions.GatewayTimeout
36HTTPVersionNotSupported = werkzeug.exceptions.HTTPVersionNotSupported
38HTTPException = werkzeug.exceptions.HTTPException
41def from_exception(exc: Exception) -> HTTPException:
42 """Convert an exception to an HTTP exception.
44 HTTP exceptions are returned as is. ``gws.NotFoundError``, ``gws.ForbiddenError``,
45 ``gws.BadRequestError``, ``gws.TooManyRequestsError`` and ``gws.ResponseTooLargeError``
46 are converted to 404, 403, 400, 429 and 409 and logged as warnings. Other exceptions
47 are logged with the traceback and converted to 500. The original exception is
48 set as the cause of the new one.
50 Args:
51 exc: An exception.
53 Returns:
54 An HTTP exception.
55 """
57 if isinstance(exc, HTTPException):
58 return exc
60 e = None
62 if isinstance(exc, gws.NotFoundError):
63 e = NotFound()
64 elif isinstance(exc, gws.ForbiddenError):
65 e = Forbidden()
66 elif isinstance(exc, gws.BadRequestError):
67 e = BadRequest()
68 elif isinstance(exc, gws.TooManyRequestsError):
69 e = TooManyRequests(retry_after=exc.retryAfter or None)
70 elif isinstance(exc, gws.ResponseTooLargeError):
71 e = Conflict()
73 if e:
74 gws.log.warning(f'HTTPException: {e.code} cause={exc!r}')
75 else:
76 gws.log.exception()
77 e = InternalServerError()
79 e.__cause__ = exc
80 return e