forked from steemit/jussi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors.py
336 lines (262 loc) · 9.99 KB
/
errors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# -*- coding: utf-8 -*-
import asyncio
import concurrent.futures
import logging
import uuid
from typing import Optional
from typing import Union
import sanic.exceptions
import structlog
from funcy.decorators import decorator
from sanic import response
from .typedefs import HTTPRequest
from .typedefs import HTTPResponse
from .typedefs import JrpcRequest
from .typedefs import JrpcResponse
from .typedefs import WebApp
logger = structlog.get_logger(__name__)
# pylint: disable=bare-except
class Default(dict):
"""helper for format strings in Exception messages"""
def __missing__(self, key):
return key
def setup_error_handlers(app: WebApp) -> WebApp:
# pylint: disable=unused-variable
@app.exception(sanic.exceptions.RequestTimeout)
def handle_request_timeout_errors(request: HTTPRequest,
exception: sanic.exceptions.RequestTimeout) -> Optional[HTTPResponse]:
if not request:
return None
return RequestTimeoutError(http_request=request,
jrpc_request=request.jsonrpc,
exception=exception).to_sanic_response()
@app.exception(sanic.exceptions.ServiceUnavailable)
def handle_response_timeout_errors(request: HTTPRequest,
exception: sanic.exceptions.ServiceUnavailable) -> Optional[HTTPResponse]:
if not request:
return None
return ResponseTimeoutError(http_request=request,
jrpc_request=request.jsonrpc,
exception=exception).to_sanic_response()
@app.exception(concurrent.futures.CancelledError,
concurrent.futures.TimeoutError,
asyncio.TimeoutError)
def handle_async_timeout_errors(request: HTTPRequest,
exception: Exception) -> Optional[HTTPResponse]:
if not request:
return None
return RequestTimeoutError(http_request=request,
jrpc_request=request.jsonrpc,
exception=exception).to_sanic_response()
@app.exception(JsonRpcError)
def handle_jsonrpc_error(request: HTTPRequest,
exception: JsonRpcError) -> HTTPResponse:
if not exception.http_request:
exception.add_http_request(request)
return exception.to_sanic_response()
# pylint: enable=unused-argument
@app.exception(Exception)
def handle_errors(request: HTTPRequest,
exception: Exception) -> HTTPResponse:
"""handles all errors"""
if isinstance(exception, InvalidRequest):
return InvalidRequest(http_request=request,
exception=exception,
reason=exception.message).to_sanic_response()
return JsonRpcError(http_request=request,
exception=exception).to_sanic_response()
return app
@decorator
async def handle_middleware_exceptions(call):
"""Return response when exceptions happened in middleware
"""
try:
return await call()
except Exception as e:
# pylint: disable=no-member
if isinstance(e, JsonRpcError):
if not e.http_request:
e.add_http_request(call.request)
return e.to_sanic_response()
return JsonRpcError(http_request=call.request,
exception=e).to_sanic_response()
# pylint: disable=too-many-instance-attributes,too-many-arguments
class JussiInteralError(Exception):
"""Base class for errors that Jussi logs, but don't
result in JSONRPC error responses
"""
message = 'Jussi internal error'
def __init__(self,
http_request: HTTPRequest = None,
jrpc_request: JrpcRequest=None,
jrpc_response: JrpcResponse=None,
exception: Exception = None,
log_traceback: bool = False,
error_logger: logging.Logger = None,
**kwargs) -> None:
self.kwargs = kwargs
super().__init__(self.format_message())
self.http_request = http_request
self.jsonrpc_request = jrpc_request
self.jsonrpc_response = jrpc_response
self.exception = exception
self.log_traceback = log_traceback
self.logger = error_logger or logger
self.kwargs = kwargs
self.error_id = str(uuid.uuid4())
def format_message(self, kwargs: dict=None) ->str:
kwargs = kwargs or self.kwargs
try:
return self.message.format_map(Default(**kwargs))
except Exception:
return self.message
@property
def amzn_trace_id(self) -> Optional[str]:
try:
return self.jsonrpc_request.amzn_trace_id
except BaseException:
pass
try:
return self.http_request.headers['X-Amzn-Trace-Id']
except BaseException:
pass
@property
def jrpc_request_id(self) -> Optional[Union[str, int]]:
try:
return self.jsonrpc_request.id
except BaseException:
pass
try:
return self.http_request.jsonrpc['id']
except BaseException:
pass
try:
# pylint: disable=protected-access
return self.http_request._parsed_json['id']
except BaseException:
pass
@property
def jussi_request_id(self) -> Optional[Union[str, int]]:
try:
return self.jsonrpc_request.jussi_request_id
except BaseException:
pass
try:
return self.http_request.jussi_request_id
except BaseException:
pass
try:
return self.http_request.headers['x-jussi-request-id']
except BaseException:
pass
def add_http_request(self, http_request: HTTPRequest) -> None:
self.http_request = http_request
def add_jsonrpc_request(self, jsonrpc_request: JrpcRequest) -> None:
self.jsonrpc_request = jsonrpc_request
def add_jsonrpc_response(self, jsonrpc_response: JrpcResponse) -> None:
self.jsonrpc_response = jsonrpc_response
def to_dict(self) -> dict:
base_error = {
'message': self.format_message(),
'error_id': self.error_id,
'jrpc_request_id': self.jrpc_request_id,
'jussi_request_id': self.jussi_request_id
}
try:
base_error.update(**self.kwargs)
except Exception as e:
logger.warning('JussiInteralError kwargs serialization error', e=e)
return base_error
def log(self) -> None:
if self.log_traceback and self.exception:
self.logger.error(self.format_message(), **self.to_dict(),
exc_info=self.exception)
else:
self.logger.error(self.format_message(),
exception=self.exception,
**self.to_dict())
# pylint: enable=too-many-instance-attributes,too-many-arguments
class JsonRpcError(JussiInteralError):
"""Base class for the JsonRpc other exceptions.
"""
message = 'Internal Error'
code = -32603
# pylint: disable=too-many-arguments
def to_sanic_response(self) -> HTTPResponse:
self.log()
error = {
'jsonrpc': '2.0',
'id': self.jrpc_request_id,
'error': {
'code': self.code,
'message': self.format_message(),
'data': {
'error_id': self.error_id,
'jussi_request_id': self.jussi_request_id
}
}
}
return response.json(error, headers={'x-jussi-error-id': self.error_id})
class ParseError(JsonRpcError):
"""Raised when the request is not a valid JSON object.
"""
code = -32700
message = 'Parse error'
class InvalidRequest(JsonRpcError):
"""Raised when the request is not a valid JSON-RPC object.
"""
code = -32600
message = 'Invalid Request'
class ServerError(JsonRpcError):
"""Raised when there's an application-specific error on the server side.
"""
code = -32000
message = 'Server error'
class RequestTimeoutError(JsonRpcError):
code = 1000
message = 'Request Timeout'
def to_dict(self):
data = super().to_dict()
try:
timings = self.timings()
if timings:
data.update(**timings)
except Exception as e:
logger.info('error adding timing data to RequestTimeoutError', e=e)
return data
class ResponseTimeoutError(JsonRpcError):
code = 1050
message = 'Response Timeout'
def to_dict(self):
data = super().to_dict()
try:
timings = self.timings()
if timings:
data.update(**timings)
except Exception as e:
logger.info('error adding timing data to RequestTimeoutError', e=e)
return data
class UpstreamResponseError(JsonRpcError):
code = 1100
message = 'Upstream response error'
class InvalidNamespaceError(JsonRpcError):
code = 1200
message = 'Invalid JSONRPC method namespace {namespace}'
class InvalidNamespaceAPIError(JsonRpcError):
code = 1300
message = 'Invalid JSONRPC method namespace, unable to resolve {namespace}.{api}'
class InvalidUpstreamHost(JsonRpcError):
code = 1400
message = 'Invalid/unresolvable upstream hostname {url}'
class InvalidUpstreamURL(JsonRpcError):
code = 1500
message = 'Invalid/unhealthy upstream {url} {reason}'
class JsonRpcBatchSizeError(JsonRpcError):
code = 1600
message = 'JSONRPC batch size of {jrpc_batch_size} exceeds {jrpc_batch_size_limit}'
class JussiLimitsError(JsonRpcError):
code = 1700
message = 'Request exceeded limit'
class JussiCustomJsonOpLengthError(JsonRpcError):
code = 1800
message = 'Custom JSON operation size limit of {size_limit} exceeded'