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.1, created at 2024-09-03 13:41 +0000

1class HTTPException(Exception): 

2 """ 

3 Base-Class for all Exceptions that should match to an http error-code 

4 """ 

5 

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 

14 

15 from .i18n import translate # fixme: This might be done better 

16 self.name = name 

17 self.descr = str(translate(descr)) 

18 

19 def process(self): 

20 pass 

21 

22 

23class BadRequest(HTTPException): 

24 """ 

25 BadRequest 

26 """ 

27 

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) 

30 

31 

32class Redirect(HTTPException): 

33 """ 

34 Causes an 303 - See Other (or 302 - Found if requested / 301 - Moved Permanently) redirect 

35 """ 

36 

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 

43 

44 

45class Unauthorized(HTTPException): 

46 """ 

47 Unauthorized 

48 

49 Raised whenever a request hits an path protected by canAccess() or a canAdd/canEdit/... -Function inside 

50 an application returns false. 

51 """ 

52 

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) 

55 

56 

57class PaymentRequired(HTTPException): 

58 """ 

59 PaymentRequired 

60 

61 Not used inside viur.core. This status-code is reserved for further use and is currently not 

62 supported by clients. 

63 """ 

64 

65 def __init__(self, descr: str = "Payment Required"): 

66 super(PaymentRequired, self).__init__(status=402, name="Payment Required", descr=descr) 

67 

68 

69class Forbidden(HTTPException): 

70 """ 

71 Forbidden 

72 

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 """ 

76 

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) 

79 

80 

81class NotFound(HTTPException): 

82 """ 

83 NotFound 

84 

85 Usually raised in view() methods from application if the given key is invalid. 

86 """ 

87 

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) 

90 

91 

92class MethodNotAllowed(HTTPException): 

93 """ 

94 MethodNotAllowed 

95 

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 """ 

99 

100 def __init__(self, descr: str = "Method Not Allowed"): 

101 super(MethodNotAllowed, self).__init__(status=405, name="Method Not Allowed", descr=descr) 

102 

103 

104class NotAcceptable(HTTPException): 

105 """ 

106 NotAcceptable 

107 

108 Signals that the parameters supplied doesn't match the function signature 

109 """ 

110 

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) 

113 

114 

115class RequestTimeout(HTTPException): 

116 """ 

117 RequestTimeout 

118 

119 This must be used for the task api to indicate it should retry 

120 """ 

121 

122 def __init__(self, descr: str = "The request has timed out."): 

123 super(RequestTimeout, self).__init__(status=408, name="Request Timeout", descr=descr) 

124 

125 

126class Gone(HTTPException): 

127 """ 

128 Gone 

129 

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. 

136 

137 See https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.9 

138 """ 

139 

140 def __init__(self, descr: str = "Gone"): 

141 super(Gone, self).__init__(status=410, name="Gone", descr=descr) 

142 

143 

144class PreconditionFailed(HTTPException): 

145 """ 

146 PreconditionFailed 

147 

148 Mostly caused by a missing/invalid securitykey. 

149 """ 

150 

151 def __init__(self, descr: str = "Precondition Failed"): 

152 super(PreconditionFailed, self).__init__(status=412, name="Precondition Failed", descr=descr) 

153 

154 

155class RequestTooLarge(HTTPException): 

156 """ 

157 RequestTooLarge 

158 

159 Not used inside viur.core 

160 """ 

161 

162 def __init__(self, descr: str = "Request Too Large"): 

163 super(RequestTooLarge, self).__init__(status=413, name="Request Too Large", descr=descr) 

164 

165 

166class Locked(HTTPException): 

167 """ 

168 Locked 

169 

170 Raised if a resource cannot be deleted due to incomming relational locks 

171 """ 

172 

173 def __init__(self, descr: str = "Ressource is Locked"): 

174 super(Locked, self).__init__(status=423, name="Ressource is Locked", descr=descr) 

175 

176 

177class TooManyRequests(HTTPException): 

178 """ 

179 Too Many Requests 

180 

181 The 429 status code indicates that the user has sent too many 

182 requests in a given amount of time ("rate limiting"). 

183 """ 

184 

185 def __init__(self, descr: str = "Too Many Requests"): 

186 super(TooManyRequests, self).__init__(status=429, name="Too Many Requests", descr=descr) 

187 

188 

189class UnprocessableEntity(HTTPException): 

190 """ 

191 Unprocessable Entity 

192 

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 

202 

203 See https://www.rfc-editor.org/rfc/rfc4918#section-11.2 

204 """ 

205 

206 def __init__(self, descr: str = "Unprocessable Entity"): 

207 super().__init__(status=422, name="Unprocessable Entity", descr=descr) 

208 

209 

210class Censored(HTTPException): 

211 """ 

212 Censored 

213 

214 Not used inside viur.core 

215 """ 

216 

217 def __init__(self, descr: str = "Unavailable For Legal Reasons"): 

218 super(Censored, self).__init__(status=451, name="Unavailable For Legal Reasons", descr=descr) 

219 

220 

221class InternalServerError(HTTPException): 

222 """ 

223 InternalServerError 

224 

225 The catch-all error raised by the server if your code raises any python-exception not deriving from 

226 HTTPException 

227 """ 

228 

229 def __init__(self, descr: str = "Internal Server Error"): 

230 super(InternalServerError, self).__init__(status=500, name="Internal Server Error", descr=descr) 

231 

232 

233class NotImplemented(HTTPException): 

234 """ 

235 NotImplemented 

236 

237 Not really implemented at the moment :) 

238 """ 

239 

240 def __init__(self, descr: str = "Not Implemented"): 

241 super(NotImplemented, self).__init__(status=501, name="Not Implemented", descr=descr) 

242 

243 

244class BadGateway(HTTPException): 

245 """ 

246 BadGateway 

247 

248 Not used inside viur.core 

249 """ 

250 

251 def __init__(self, descr: str = "Bad Gateway"): 

252 super(BadGateway, self).__init__(status=502, name="Bad Gateway", descr=descr) 

253 

254 

255class ServiceUnavailable(HTTPException): 

256 """ 

257 ServiceUnavailable 

258 

259 Not used inside viur.core 

260 """ 

261 

262 def __init__(self, descr: str = "Service Unavailable"): 

263 super(ServiceUnavailable, self).__init__(status=503, name="Service Unavailable", descr=descr)