diff --git a/lookup.py b/lookup.py
index fc3a2f6..62fde8c 100755
--- a/lookup.py
+++ b/lookup.py
@@ -1,11 +1,15 @@
 #!/usr/bin/python3
 """Reverse geocode: convert lat/lon to Wikidata item & Wikimedia Commons category."""
 
+import inspect
 import random
 import socket
+import sys
+import traceback
 import typing
 
 import sqlalchemy.exc
+import werkzeug.debug.tbtools
 from flask import Flask, jsonify, redirect, render_template, request, url_for
 from sqlalchemy.orm.query import Query
 from werkzeug.wrappers import Response
@@ -24,6 +28,33 @@ Tags = typing.Mapping[str, str]
 logging_enabled = True
 
 
+@app.errorhandler(werkzeug.exceptions.InternalServerError)
+def exception_handler(e: werkzeug.exceptions.InternalServerError) -> tuple[str, int]:
+    """Handle exception."""
+    exec_type, exc_value, current_traceback = sys.exc_info()
+    assert exc_value
+    tb = werkzeug.debug.tbtools.DebugTraceback(exc_value)
+
+    summary = tb.render_traceback_html(include_title=False)
+    exc_lines = "".join(tb._te.format_exception_only())
+
+    last_frame = list(traceback.walk_tb(current_traceback))[-1][0]
+    last_frame_args = inspect.getargs(last_frame.f_code)
+
+    return (
+        render_template(
+            "show_error.html",
+            plaintext=tb.render_traceback_text(),
+            exception=exc_lines,
+            exception_type=tb._te.exc_type.__name__,
+            summary=summary,
+            last_frame=last_frame,
+            last_frame_args=last_frame_args,
+        ),
+        500,
+    )
+
+
 def get_random_lat_lon() -> tuple[float, float]:
     """Select random lat/lon within the UK."""
     south, east = 50.8520, 0.3536
diff --git a/templates/show_error.html b/templates/show_error.html
new file mode 100644
index 0000000..dd79c3c
--- /dev/null
+++ b/templates/show_error.html
@@ -0,0 +1,23 @@
+{% extends "base.html" %}
+
+{% block style %}
+<link rel="stylesheet" href="{{url_for('static', filename='css/exception.css')}}" />
+{% endblock %}
+
+{% block content %}
+<div class="p-2">
+
+<h1>Software error: {{ exception_type }}</h1>
+<div>
+  <pre>{{ exception }}</pre>
+</div>
+
+<h2 class="traceback">Traceback <em>(most recent call last)</em></h2>
+{{ summary | safe }}
+
+<p>Error in function "{{ last_frame.f_code.co_name }}": {{ last_frame_args | pprint }}</p>
+<pre>{{ last_frame.f_locals | pprint }}</pre>
+
+</div>
+
+{% endblock %}