Coverage for /home/runner/work/viur-core/viur-core/viur/src/viur/core/errors.py: 58%
72 statements
« prev ^ index » next coverage.py v7.6.3, created at 2024-10-16 22:16 +0000
« prev ^ index » next coverage.py v7.6.3, created at 2024-10-16 22:16 +0000
1class HTTPException(Exception):
2 """
3 Base-Class for all Exceptions that should match to an http error-code
4 """
6 def __init__(self, status: int, name: str, descr: str):
7 """
8 :param status: The desired http error-code (404, 500, ...)
9 :param name: Name as of RFC 2616
10 :param descr: Human-readable description of that error
11 """
12 super(HTTPException, self).__init__()
13 self.status = status
15 from .i18n import translate # fixme: This might be done better
16 self.name = name
17 self.descr = str(translate(descr))
19 def process(self):
20 pass
23class BadRequest(HTTPException):
24 """
25 BadRequest
26 """
28 def __init__(self, descr: str = "The request your browser sent cannot be fulfilled due to bad syntax."):
29 super(BadRequest, self).__init__(status=400, name="Bad Request", descr=descr)
32class Redirect(HTTPException):
33 """
34 Causes an 303 - See Other (or 302 - Found if requested / 301 - Moved Permanently) redirect
35 """
37 def __init__(self, url: str, descr: str = "Redirect", status: int = 303):
38 if not isinstance(status, int) or status not in {301, 302, 303, 307, 308}:
39 raise ValueError(f"Invalid status {status!r}. Only the status codes 301, 302, 303, 307 and 308 "
40 "are valid for a redirect.")
41 super(Redirect, self).__init__(status=status, name="Redirect", descr=descr)
42 self.url = url
45class Unauthorized(HTTPException):
46 """
47 Unauthorized
49 Raised whenever a request hits an path protected by canAccess() or a canAdd/canEdit/... -Function inside
50 an application returns false.
51 """
53 def __init__(self, descr: str = "The resource is protected and you don't have the permissions."):
54 super(Unauthorized, self).__init__(status=401, name="Unauthorized", descr=descr)
57class PaymentRequired(HTTPException):
58 """
59 PaymentRequired
61 Not used inside viur.core. This status-code is reserved for further use and is currently not
62 supported by clients.
63 """
65 def __init__(self, descr: str = "Payment Required"):
66 super(PaymentRequired, self).__init__(status=402, name="Payment Required", descr=descr)
69class Forbidden(HTTPException):
70 """
71 Forbidden
73 Not used inside viur.core. May be utilized in the future to distinguish between requests from
74 guests and users, who are logged in but don't have the permission.
75 """
77 def __init__(self, descr: str = "The resource is protected and you don't have the permissions."):
78 super(Forbidden, self).__init__(status=403, name="Forbidden", descr=descr)
81class NotFound(HTTPException):
82 """
83 NotFound
85 Usually raised in view() methods from application if the given key is invalid.
86 """
88 def __init__(self, descr: str = "The requested resource could not be found."):
89 super(NotFound, self).__init__(status=404, name="Not Found", descr=descr)
92class MethodNotAllowed(HTTPException):
93 """
94 MethodNotAllowed
96 Raised if a function is accessed which doesn't have the @exposed / @internalExposed decorator or
97 if the request arrived using get, but the function has the @forcePost flag.
98 """
100 def __init__(self, descr: str = "Method Not Allowed"):
101 super(MethodNotAllowed, self).__init__(status=405, name="Method Not Allowed", descr=descr)
104class NotAcceptable(HTTPException):
105 """
106 NotAcceptable
108 Signals that the parameters supplied doesn't match the function signature
109 """
111 def __init__(self, descr: str = "The request cannot be processed due to missing or invalid parameters."):
112 super(NotAcceptable, self).__init__(status=406, name="Not Acceptable", descr=descr)
115class RequestTimeout(HTTPException):
116 """
117 RequestTimeout
119 This must be used for the task api to indicate it should retry
120 """
122 def __init__(self, descr: str = "The request has timed out."):
123 super(RequestTimeout, self).__init__(status=408, name="Request Timeout", descr=descr)
126class Gone(HTTPException):
127 """
128 Gone
130 The 410 (Gone) status code indicates that access to the target
131 resource is no longer available at the origin server and that this
132 condition is likely to be permanent. If the origin server does not
133 know, or has no facility to determine, whether or not the condition
134 is permanent, the status code 404 (Not Found) ought to be used
135 instead.
137 See https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.9
138 """
140 def __init__(self, descr: str = "Gone"):
141 super(Gone, self).__init__(status=410, name="Gone", descr=descr)
144class PreconditionFailed(HTTPException):
145 """
146 PreconditionFailed
148 Mostly caused by a missing/invalid securitykey.
149 """
151 def __init__(self, descr: str = "Precondition Failed"):
152 super(PreconditionFailed, self).__init__(status=412, name="Precondition Failed", descr=descr)
155class RequestTooLarge(HTTPException):
156 """
157 RequestTooLarge
159 Not used inside viur.core
160 """
162 def __init__(self, descr: str = "Request Too Large"):
163 super(RequestTooLarge, self).__init__(status=413, name="Request Too Large", descr=descr)
166class Locked(HTTPException):
167 """
168 Locked
170 Raised if a resource cannot be deleted due to incomming relational locks
171 """
173 def __init__(self, descr: str = "Ressource is Locked"):
174 super(Locked, self).__init__(status=423, name="Ressource is Locked", descr=descr)
177class TooManyRequests(HTTPException):
178 """
179 Too Many Requests
181 The 429 status code indicates that the user has sent too many
182 requests in a given amount of time ("rate limiting").
183 """
185 def __init__(self, descr: str = "Too Many Requests"):
186 super(TooManyRequests, self).__init__(status=429, name="Too Many Requests", descr=descr)
189class UnprocessableEntity(HTTPException):
190 """
191 Unprocessable Entity
193 The 422 (Unprocessable Entity) status code means the server
194 understands the content type of the request entity (hence a
195 415 (Unsupported Media Type) status code is inappropriate), and the
196 syntax of the request entity is correct (thus a 400 (Bad Request)
197 status code is inappropriate) but was unable to process the contained
198 instructions.
199 For example, this error condition may occur if an XML
200 request body contains well-formed (i.e., syntactically correct), but
201 semantically erroneous, XML instructions
203 See https://www.rfc-editor.org/rfc/rfc4918#section-11.2
204 """
206 def __init__(self, descr: str = "Unprocessable Entity"):
207 super().__init__(status=422, name="Unprocessable Entity", descr=descr)
210class Censored(HTTPException):
211 """
212 Censored
214 Not used inside viur.core
215 """
217 def __init__(self, descr: str = "Unavailable For Legal Reasons"):
218 super(Censored, self).__init__(status=451, name="Unavailable For Legal Reasons", descr=descr)
221class InternalServerError(HTTPException):
222 """
223 InternalServerError
225 The catch-all error raised by the server if your code raises any python-exception not deriving from
226 HTTPException
227 """
229 def __init__(self, descr: str = "Internal Server Error"):
230 super(InternalServerError, self).__init__(status=500, name="Internal Server Error", descr=descr)
233class NotImplemented(HTTPException):
234 """
235 NotImplemented
237 Not really implemented at the moment :)
238 """
240 def __init__(self, descr: str = "Not Implemented"):
241 super(NotImplemented, self).__init__(status=501, name="Not Implemented", descr=descr)
244class BadGateway(HTTPException):
245 """
246 BadGateway
248 Not used inside viur.core
249 """
251 def __init__(self, descr: str = "Bad Gateway"):
252 super(BadGateway, self).__init__(status=502, name="Bad Gateway", descr=descr)
255class ServiceUnavailable(HTTPException):
256 """
257 ServiceUnavailable
259 Not used inside viur.core
260 """
262 def __init__(self, descr: str = "Service Unavailable"):
263 super(ServiceUnavailable, self).__init__(status=503, name="Service Unavailable", descr=descr)