Coverage for gws-app/gws/base/web/error.py: 67%

51 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-17 01:37 +0200

1from typing import Optional 

2 

3import gws 

4import werkzeug.exceptions 

5 

6BadRequest = werkzeug.exceptions.BadRequest 

7Unauthorized = werkzeug.exceptions.Unauthorized 

8Forbidden = werkzeug.exceptions.Forbidden 

9NotFound = werkzeug.exceptions.NotFound 

10MethodNotAllowed = werkzeug.exceptions.MethodNotAllowed 

11NotAcceptable = werkzeug.exceptions.NotAcceptable 

12RequestTimeout = werkzeug.exceptions.RequestTimeout 

13Conflict = werkzeug.exceptions.Conflict 

14Gone = werkzeug.exceptions.Gone 

15LengthRequired = werkzeug.exceptions.LengthRequired 

16PreconditionFailed = werkzeug.exceptions.PreconditionFailed 

17RequestEntityTooLarge = werkzeug.exceptions.RequestEntityTooLarge 

18RequestURITooLarge = werkzeug.exceptions.RequestURITooLarge 

19UnsupportedMediaType = werkzeug.exceptions.UnsupportedMediaType 

20RequestedRangeNotSatisfiable = werkzeug.exceptions.RequestedRangeNotSatisfiable 

21ExpectationFailed = werkzeug.exceptions.ExpectationFailed 

22ImATeapot = werkzeug.exceptions.ImATeapot 

23UnprocessableEntity = werkzeug.exceptions.UnprocessableEntity 

24Locked = werkzeug.exceptions.Locked 

25PreconditionRequired = werkzeug.exceptions.PreconditionRequired 

26TooManyRequests = werkzeug.exceptions.TooManyRequests 

27RequestHeaderFieldsTooLarge = werkzeug.exceptions.RequestHeaderFieldsTooLarge 

28UnavailableForLegalReasons = werkzeug.exceptions.UnavailableForLegalReasons 

29InternalServerError = werkzeug.exceptions.InternalServerError 

30NotImplemented = werkzeug.exceptions.NotImplemented 

31BadGateway = werkzeug.exceptions.BadGateway 

32ServiceUnavailable = werkzeug.exceptions.ServiceUnavailable 

33GatewayTimeout = werkzeug.exceptions.GatewayTimeout 

34HTTPVersionNotSupported = werkzeug.exceptions.HTTPVersionNotSupported 

35 

36HTTPException = werkzeug.exceptions.HTTPException 

37 

38 

39def from_exception(exc: Exception) -> HTTPException: 

40 """Convert generic errors to http errors.""" 

41 

42 if isinstance(exc, HTTPException): 

43 return exc 

44 

45 e = None 

46 

47 if isinstance(exc, gws.NotFoundError): 

48 e = NotFound() 

49 elif isinstance(exc, gws.ForbiddenError): 

50 e = Forbidden() 

51 elif isinstance(exc, gws.BadRequestError): 

52 e = BadRequest() 

53 elif isinstance(exc, gws.ResponseTooLargeError): 

54 e = Conflict() 

55 

56 if e: 

57 gws.log.warning(f'HTTPException: {e.code} cause={exc!r}') 

58 else: 

59 gws.log.exception() 

60 e = InternalServerError() 

61 

62 e.__cause__ = exc 

63 return e