mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Improved error handling
Invalid SQL now shows a special error.html template, and is covered by tests.
This commit is contained in:
parent
ff2ab9dc7d
commit
666aa03253
4 changed files with 53 additions and 20 deletions
|
|
@ -140,6 +140,8 @@ class BaseView(HTTPMethodView):
|
|||
as_json = False
|
||||
extra_template_data = {}
|
||||
start = time.time()
|
||||
template = self.template
|
||||
status_code = 200
|
||||
try:
|
||||
data, extra_template_data = await self.data(
|
||||
request, name, hash, **kwargs
|
||||
|
|
@ -148,7 +150,11 @@ class BaseView(HTTPMethodView):
|
|||
data = {
|
||||
'ok': False,
|
||||
'error': str(e),
|
||||
'database': name,
|
||||
'database_hash': hash,
|
||||
}
|
||||
template = 'error.html'
|
||||
status_code = 400
|
||||
end = time.time()
|
||||
data['query_ms'] = (end - start) * 1000
|
||||
if as_json:
|
||||
|
|
@ -165,6 +171,7 @@ class BaseView(HTTPMethodView):
|
|||
json.dumps(
|
||||
data, cls=CustomJSONEncoder
|
||||
),
|
||||
status=status_code,
|
||||
content_type='application/json',
|
||||
headers={
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
|
|
@ -180,10 +187,11 @@ class BaseView(HTTPMethodView):
|
|||
'url_jsono': path_with_ext(request, '.jsono'),
|
||||
}}
|
||||
r = self.jinja.render(
|
||||
self.template,
|
||||
template,
|
||||
request,
|
||||
**context,
|
||||
)
|
||||
r.status = status_code
|
||||
# Set far-future cache expiry
|
||||
if self.cache_headers:
|
||||
r.headers['Cache-Control'] = 'max-age={}'.format(
|
||||
|
|
|
|||
|
|
@ -8,9 +8,6 @@
|
|||
</head>
|
||||
<body>
|
||||
|
||||
{% if error %}
|
||||
<div style="padding: 1em; margin: 1em; border: 3px solid red;">{{ error }}</div>
|
||||
{% endif %}
|
||||
{% block content %}
|
||||
{% endblock %}
|
||||
|
||||
|
|
|
|||
14
datasette/templates/error.html
Normal file
14
datasette/templates/error.html
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}{{ database }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="hd"><a href="/">home</a> / <a href="/{{ database }}-{{ database_hash }}">{{ database }}</a></div>
|
||||
|
||||
<h1 style="padding-left: 10px; border-left: 10px solid #{{ database_hash and database_hash[:6] }}">{{ database }}</h1>
|
||||
|
||||
{% if error %}
|
||||
<div style="padding: 1em; margin: 1em; border: 3px solid red;">{{ error }}</div>
|
||||
{% endif %}
|
||||
|
||||
{% endblock %}
|
||||
Loading…
Add table
Add a link
Reference in a new issue