diff --git a/app/templates/error.html b/app/templates/error.html new file mode 100644 index 000000000..13b391377 --- /dev/null +++ b/app/templates/error.html @@ -0,0 +1,8 @@ +{% extends "base.html" %} +{% from '_page.html' import page_head %} +{# A page that is not there, or a request that failed: its title and the way back, never the error's details. #} +{% block title %}{{ title }} — Nullius{% endblock %} +{% block body %} +{{ page_head(title) }} +

← Runs

+{% endblock %} diff --git a/app/ui_tests/test_routes.py b/app/ui_tests/test_routes.py index 4c51587fc..7b3229ff5 100644 --- a/app/ui_tests/test_routes.py +++ b/app/ui_tests/test_routes.py @@ -168,6 +168,26 @@ def test_home_opens_runs_with_or_without_active_workspace(self): self.assertIn('aria-label="Pages"', html) self.assertNotIn("So who's actually best?", html) + def test_an_error_renders_in_the_layout_without_its_details(self): + for path in ("/no-such-page", "/job/no-such-job", "/job/active-run?workspace=gone"): + with self.subTest(path=path): + response = self.client.get(path) + self.addCleanup(response.close) + self.assertEqual(response.status_code, 404) + html = response.get_data(as_text=True) + self.assertIn("

Not found

", html) + self.assertIn('aria-label="Pages"', html) + self.assertIn('href="/runs"', html) + with patch.dict(web.app.config, PROPAGATE_EXCEPTIONS=False), \ + patch.object(web.shared, "_jobs_for_request", side_effect=RuntimeError("secret detail")): + response = self.client.get("/job/active-run") + self.addCleanup(response.close) + self.assertEqual(response.status_code, 500) + html = response.get_data(as_text=True) + self.assertIn("

Server error

", html) + self.assertIn('aria-label="Pages"', html) + self.assertNotIn("secret detail", html) + def test_command_builder_serves_catalogue_and_adapter_metadata(self): html = self.get_ok("/new-run").get_data(as_text=True) self.assertIn('id="nullius-builder"', html) diff --git a/app/web/filters.py b/app/web/filters.py index 217cdacb0..5e579e0cb 100644 --- a/app/web/filters.py +++ b/app/web/filters.py @@ -1,11 +1,11 @@ -"""Template filters and the context every page renders with.""" +"""Template filters, the context every page renders with, and the pages for a 404 and a 500.""" from __future__ import annotations import json import re from pathlib import Path -from flask import url_for +from flask import g, render_template, url_for from nullius import importers as harnesses, providers from nullius.judge import client as judge_client @@ -213,7 +213,7 @@ def _inject(): "INFRA": classify.INFRA_OUTCOMES, "ORDER": classify.ORDER, "workspace": shared.STATE["workspace"], - "record_workspace": shared._record_workspace(), + "record_workspace": None if g.get("error_page") else shared._record_workspace(), # For the rail's switcher: the most recent few, and a stable hue per # name so a workspace keeps its colour across pages and restarts. "workspaces": workspace.list_all(shared.WS_HOME)[:8], @@ -235,3 +235,18 @@ def _inject(): # The runs list's Columns menu; the template reads it by this name. "COLUMNS": shared.COLUMNS, } + + +def _error_page(title, status): + g.error_page = True # an unknown ?workspace= may be what failed: the page does not read it again + return render_template("error.html", title=title), status + + +@app.errorhandler(404) +def _not_found(_): + return _error_page("Not found", 404) + + +@app.errorhandler(500) +def _server_error(_): + return _error_page("Server error", 500)