MCPcopy
hub / github.com/aio-libs/aiohttp / handle_error

Method handle_error

aiohttp/server.py:318–369  ·  view source on GitHub ↗

Handle errors. Returns HTTP response with specific status code. Logs additional information. It always closes current connection.

(self, status=500, message=None,
                     payload=None, exc=None, headers=None, reason=None)

Source from the content-addressed store, hash-verified

316 return
317
318 def handle_error(self, status=500, message=None,
319 payload=None, exc=None, headers=None, reason=None):
320 """Handle errors.
321
322 Returns HTTP response with specific status code. Logs additional
323 information. It always closes current connection."""
324 now = self._loop.time()
325 try:
326 if self._request_handler is None:
327 # client has been disconnected during writing.
328 return ()
329
330 if status == 500:
331 self.log_exception("Error handling request")
332
333 try:
334 if reason is None or reason == '':
335 reason, msg = RESPONSES[status]
336 else:
337 msg = reason
338 except KeyError:
339 status = 500
340 reason, msg = '???', ''
341
342 if self.debug and exc is not None:
343 try:
344 tb = traceback.format_exc()
345 tb = html_escape(tb)
346 msg += '<br><h2>Traceback:</h2>\n<pre>{}</pre>'.format(tb)
347 except:
348 pass
349
350 html = DEFAULT_ERROR_MESSAGE.format(
351 status=status, reason=reason, message=msg).encode('utf-8')
352
353 response = aiohttp.Response(self.writer, status, close=True)
354 response.add_header(hdrs.CONTENT_TYPE, 'text/html; charset=utf-8')
355 response.add_header(hdrs.CONTENT_LENGTH, str(len(html)))
356 if headers is not None:
357 for name, value in headers:
358 response.add_header(name, value)
359 response.send_headers()
360
361 response.write(html)
362 # disable CORK, enable NODELAY if needed
363 self.writer.set_tcp_nodelay(True)
364 drain = response.write_eof()
365
366 self.log_access(message, None, response, self._loop.time() - now)
367 return drain
368 finally:
369 self.keep_alive(False)
370
371 def handle_request(self, message, payload):
372 """Handle a single HTTP request.

Callers 7

startMethod · 0.95
test_handle_errorFunction · 0.80
test_handle_error__utfFunction · 0.80
test_handle_error_debugFunction · 0.80
test_handle_error_500Function · 0.80

Calls 9

log_exceptionMethod · 0.95
write_eofMethod · 0.95
log_accessMethod · 0.95
keep_aliveMethod · 0.95
encodeMethod · 0.80
add_headerMethod · 0.80
send_headersMethod · 0.80
writeMethod · 0.45
set_tcp_nodelayMethod · 0.45

Tested by 6

test_handle_errorFunction · 0.64
test_handle_error__utfFunction · 0.64
test_handle_error_debugFunction · 0.64
test_handle_error_500Function · 0.64