From 57ca524dfb3ac1aa26505b52c1a340cc556b7078 Mon Sep 17 00:00:00 2001 From: Souheil Chelfouh Date: Fri, 24 Jul 2020 12:23:36 +0200 Subject: [PATCH 1/3] Fist attempt at a pluggable error reporting --- examples/misc.py | 11 ++++++++++- src/trinket/app.py | 15 +++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/examples/misc.py b/examples/misc.py index 14d3e95..7075ff5 100644 --- a/examples/misc.py +++ b/examples/misc.py @@ -4,9 +4,18 @@ from trinket import Trinket, Response from trinket.response import file_iterator from trinket.extensions import logger +from trinket.http import HTTPError -bauble = logger(Trinket()) +class MyTrinket(Trinket): + + async def on_error(self, http_code, message): + if http_code == 404: + message = "This route does not exist, i'm so sorry." + raise HTTPError(http_code, message) + + +bauble = logger(MyTrinket()) @bauble.route('/') diff --git a/src/trinket/app.py b/src/trinket/app.py index 9a21211..74f9f11 100644 --- a/src/trinket/app.py +++ b/src/trinket/app.py @@ -5,7 +5,7 @@ from autoroutes import Routes from trinket.handler import request_handler -from trinket.http import HTTPStatus, HTTPError +from trinket.http import HTTPCode, HTTPStatus, HTTPError from trinket.lifecycle import handler_events from trinket.proto import Application from trinket.request import Request @@ -25,21 +25,28 @@ def __init__(self): self.websockets = set() self.hooks = defaultdict(list) + async def on_error(self, http_code: HTTPCode, message: str=None): + """This async method allows the customization of errors. + If you need an async log or report or whatever, this is the + place to do it. + """ + raise HTTPError(HTTPStatus.NOT_FOUND, message) + async def lookup(self, request: Request): payload, params = self.routes.match(request.path) if not payload: - raise HTTPError(HTTPStatus.NOT_FOUND, request.path) + return await self.on_error(404, request.path) # Uppercased in order to only consider HTTP verbs. handler = payload.get(request.method.upper(), None) if handler is None: - raise HTTPError(HTTPStatus.METHOD_NOT_ALLOWED) + return await self.on_error(HTTPStatus.METHOD_NOT_ALLOWED) # We check if the route is for a websocket handler. # If it is, we make sure we were asked for an upgrade. if payload.get('websocket', False) and not request.upgrade: - raise HTTPError( + return await self.on_error( HTTPStatus.UPGRADE_REQUIRED, 'This is a websocket endpoint, please upgrade.') From 57249af3a373536874483d024e9930687f52d511 Mon Sep 17 00:00:00 2001 From: Souheil Chelfouh Date: Fri, 24 Jul 2020 12:27:35 +0200 Subject: [PATCH 2/3] Fixed typo --- src/trinket/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/trinket/app.py b/src/trinket/app.py index 74f9f11..c48e060 100644 --- a/src/trinket/app.py +++ b/src/trinket/app.py @@ -30,7 +30,7 @@ async def on_error(self, http_code: HTTPCode, message: str=None): If you need an async log or report or whatever, this is the place to do it. """ - raise HTTPError(HTTPStatus.NOT_FOUND, message) + raise HTTPError(http_code, message) async def lookup(self, request: Request): payload, params = self.routes.match(request.path) From c4dffcbe720f1b341ca70a997090fef0d6d31476 Mon Sep 17 00:00:00 2001 From: Souheil Chelfouh Date: Fri, 24 Jul 2020 12:28:42 +0200 Subject: [PATCH 3/3] Fixed erroneous return --- examples/misc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/misc.py b/examples/misc.py index 7075ff5..08e3ae1 100644 --- a/examples/misc.py +++ b/examples/misc.py @@ -12,7 +12,7 @@ class MyTrinket(Trinket): async def on_error(self, http_code, message): if http_code == 404: message = "This route does not exist, i'm so sorry." - raise HTTPError(http_code, message) + return await super().on_error(http_code, message) bauble = logger(MyTrinket())