Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/templates/error.html
Original file line number Diff line number Diff line change
@@ -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) }}
<p><a class="btn ghost sm" href="{{ url_for('index') }}">← Runs</a></p>
{% endblock %}
20 changes: 20 additions & 0 deletions app/ui_tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("<h1>Not found</h1>", 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("<h1>Server error</h1>", 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)
Expand Down
21 changes: 18 additions & 3 deletions app/web/filters.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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],
Expand All @@ -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)
Loading