-
-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New handle_exception plugin hook, refs #1770
Also refs: - simonw/datasette-sentry#1 - simonw/datasette-show-errors#2
- Loading branch information
Showing
10 changed files
with
216 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from os import stat | ||
from datasette import hookimpl, Response | ||
|
||
|
||
@hookimpl(trylast=True) | ||
def forbidden(datasette, request, message): | ||
async def inner(): | ||
return Response.html( | ||
await datasette.render_template( | ||
"error.html", | ||
{ | ||
"title": "Forbidden", | ||
"error": message, | ||
}, | ||
request=request, | ||
), | ||
status=403, | ||
) | ||
|
||
return inner |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from datasette import hookimpl, Response | ||
from .utils import await_me_maybe, add_cors_headers | ||
from .utils.asgi import ( | ||
Base400, | ||
Forbidden, | ||
) | ||
from .views.base import DatasetteError | ||
from markupsafe import Markup | ||
import pdb | ||
import traceback | ||
from .plugins import pm | ||
|
||
try: | ||
import rich | ||
except ImportError: | ||
rich = None | ||
|
||
|
||
@hookimpl(trylast=True) | ||
def handle_exception(datasette, request, exception): | ||
async def inner(): | ||
if datasette.pdb: | ||
pdb.post_mortem(exception.__traceback__) | ||
|
||
if rich is not None: | ||
rich.get_console().print_exception(show_locals=True) | ||
|
||
title = None | ||
if isinstance(exception, Base400): | ||
status = exception.status | ||
info = {} | ||
message = exception.args[0] | ||
elif isinstance(exception, DatasetteError): | ||
status = exception.status | ||
info = exception.error_dict | ||
message = exception.message | ||
if exception.message_is_html: | ||
message = Markup(message) | ||
title = exception.title | ||
else: | ||
status = 500 | ||
info = {} | ||
message = str(exception) | ||
traceback.print_exc() | ||
templates = [f"{status}.html", "error.html"] | ||
info.update( | ||
{ | ||
"ok": False, | ||
"error": message, | ||
"status": status, | ||
"title": title, | ||
} | ||
) | ||
headers = {} | ||
if datasette.cors: | ||
add_cors_headers(headers) | ||
if request.path.split("?")[0].endswith(".json"): | ||
return Response.json(info, status=status, headers=headers) | ||
else: | ||
template = datasette.jinja_env.select_template(templates) | ||
return Response.html( | ||
await template.render_async( | ||
dict( | ||
info, | ||
urls=datasette.urls, | ||
app_css_hash=datasette.app_css_hash(), | ||
menu_links=lambda: [], | ||
) | ||
), | ||
status=status, | ||
headers=headers, | ||
) | ||
|
||
return inner |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.