squeaky-pl / japronto

Screaming-fast Python 3.5+ HTTP toolkit integrated with pipelining HTTP server based on uvloop and picohttpparser.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to heandle 404 error ?

vadim-shadrin opened this issue · comments

I temporary replace string 76 in /japronto/app/init.py
- return request.Response(code=404, text='Not Found')
+ return request.Response(code=302,headers={'Location':'notfound'})
and addroute: /notfound
and it's work but . I whould not want to change global package

That's the right solution
You need to override the method

from japronto.router import RouteNotFoundException
import asyncio
import traceback
import sys

class App(Application):
def default_error_handler(self, request, exception):
if isinstance(exception, RouteNotFoundException):
#return request.Response(code=404, text='Not Found'
return request.Response(code=302,headers={'Location':'notfound'})
if isinstance(exception, asyncio.CancelledError):
return request.Response(code=503, text='Service unavailable')
tb = traceback.format_exception(
None, exception, exception.traceback)
tb = ''.join(tb)
print(tb, file=sys.stderr, end='')
return request.Response(
code=500,
text=tb if self._debug else 'Internal Server Error')