Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/connexion/exceptions.py: 56%
85 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 06:12 +0000
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 06:12 +0000
1"""
2This module defines Exception classes used by Connexion to generate a proper response.
3"""
5import typing as t
7from jsonschema.exceptions import ValidationError
8from starlette.exceptions import HTTPException
10from .problem import problem
13class ConnexionException(Exception):
14 pass
17class ResolverError(LookupError, ConnexionException):
18 pass
21class InvalidSpecification(ValidationError, ConnexionException):
22 pass
25class MissingMiddleware(ConnexionException):
26 pass
29# HTTP ERRORS
32class ProblemException(HTTPException, ConnexionException):
33 def __init__(
34 self,
35 *,
36 status=500,
37 title=None,
38 detail=None,
39 type=None,
40 instance=None,
41 headers=None,
42 ext=None,
43 ):
44 """
45 This exception holds arguments that are going to be passed to the
46 `connexion.problem` function to generate a proper response.
47 """
48 self.status = self.status_code = status
49 self.title = title
50 self.detail = detail
51 self.type = type
52 self.instance = instance
53 self.headers = headers
54 self.ext = ext
56 def to_problem(self):
57 return problem(
58 status=self.status,
59 title=self.title,
60 detail=self.detail,
61 type=self.type,
62 instance=self.instance,
63 headers=self.headers,
64 ext=self.ext,
65 )
68# CLIENT ERRORS (4XX)
71class ClientError(ProblemException):
72 def __init__(self, status: int = 400, title: str = None, *, detail: str = None):
73 super().__init__(status=status, title=title, detail=detail)
76class BadRequestProblem(ClientError):
77 def __init__(self, detail=None):
78 super().__init__(status=400, title="Bad Request", detail=detail)
81class ExtraParameterProblem(BadRequestProblem):
82 def __init__(self, *, param_type: str, extra_params: t.Iterable[str]):
83 detail = f"Extra {param_type} parameter(s) {','.join(extra_params)} not in spec"
84 super().__init__(detail=detail)
87class TypeValidationError(BadRequestProblem):
88 def __init__(self, schema_type: str, parameter_type: str, parameter_name: str):
89 """Exception raised when type validation fails"""
90 detail = f"Wrong type, expected '{schema_type}' for {parameter_type} parameter '{parameter_name}'"
91 super().__init__(detail=detail)
94class Unauthorized(ClientError):
96 description = (
97 "The server could not verify that you are authorized to access"
98 " the URL requested. You either supplied the wrong credentials"
99 " (e.g. a bad password), or your browser doesn't understand"
100 " how to supply the credentials required."
101 )
103 def __init__(self, detail: str = description):
104 super().__init__(401, title="Unauthorized", detail=detail)
107class OAuthProblem(Unauthorized):
108 pass
111class OAuthResponseProblem(OAuthProblem):
112 pass
115class Forbidden(HTTPException):
116 def __init__(self, detail: t.Optional[str] = None):
117 if detail is None:
118 detail = (
119 "You don't have the permission to access the requested"
120 " resource. It is either read-protected or not readable by the"
121 " server."
122 )
123 super().__init__(403, detail=detail)
126class OAuthScopeProblem(Forbidden):
127 def __init__(self, token_scopes: list, required_scopes: list) -> None:
128 self.required_scopes = required_scopes
129 self.token_scopes = token_scopes
130 detail = (
131 f"Provided token does not have the required scopes. "
132 f"Provided: {token_scopes}; Required: {required_scopes}"
133 )
134 super().__init__(detail=detail)
137class UnsupportedMediaTypeProblem(ClientError):
138 def __init__(self, detail: t.Optional[str] = None):
139 super().__init__(status=415, title="Unsupported Media Type", detail=detail)
142# SERVER ERRORS (5XX)
145class ServerError(ProblemException):
146 def __init__(
147 self,
148 status: int = 500,
149 title: t.Optional[str] = None,
150 *,
151 detail: t.Optional[str] = None,
152 ):
153 if title is None:
154 title = "Internal Server Error"
156 super().__init__(status=status, title=title, detail=detail)
159class InternalServerError(ServerError):
160 def __init__(self, detail: t.Optional[str] = None):
161 if detail is None:
162 detail = (
163 "The server encountered an internal error and was unable to complete your "
164 "request. Either the server is overloaded or there is an error in the application."
165 )
166 super().__init__(status=500, title="Internal Server Error", detail=detail)
169class NonConformingResponse(InternalServerError):
170 def __init__(self, detail: t.Optional[str] = None):
171 super().__init__(detail=detail)
174class NonConformingResponseBody(NonConformingResponse):
175 def __init__(self, detail: t.Optional[str] = None):
176 if detail is None:
177 detail = "Response body does not conform to specification"
179 super().__init__(detail=detail)
182class NonConformingResponseHeaders(NonConformingResponse):
183 def __init__(self, detail: t.Optional[str] = None):
184 if detail is None:
185 detail = "Response headers do not conform to specification"
187 super().__init__(detail=detail)
190class ResolverProblem(ServerError):
191 def __init__(self, status: int = 501, *, detail: t.Optional[str] = None):
192 super().__init__(status=status, title="Not Implemented", detail=detail)