From 414f06de742dc54b595964ad640480e027117421 Mon Sep 17 00:00:00 2001 From: Nicolas Dandrimont <nicolas@dandrimont.eu> Date: Sun, 31 Mar 2024 03:45:59 +0200 Subject: [PATCH] browse: Restore links on context-related error pages --- swh/web/browse/views/content.py | 10 ++++++---- swh/web/browse/views/directory.py | 10 ++++++---- swh/web/browse/views/release.py | 10 ++++++---- swh/web/browse/views/revision.py | 10 ++++++---- swh/web/utils/exc.py | 18 ++++++++++-------- 5 files changed, 34 insertions(+), 24 deletions(-) diff --git a/swh/web/browse/views/content.py b/swh/web/browse/views/content.py index 0528215ae..9488cf8bf 100644 --- a/swh/web/browse/views/content.py +++ b/swh/web/browse/views/content.py @@ -12,6 +12,7 @@ from typing import Any, Dict, Optional from django.http import FileResponse, HttpRequest, HttpResponse, JsonResponse from django.shortcuts import redirect, render +from django.utils.html import format_html from swh.model.hashutil import hash_to_hex from swh.model.swhids import ObjectType @@ -303,14 +304,15 @@ def content_display( raw_cnt_url = reverse( "browse-content", url_args={"query_string": query_string} ) - error_message = ( + error_message = format_html( "The Software Heritage archive has a content " "with the hash you provided but the origin " - "mentioned in your request appears broken: %s. " + "mentioned in your request appears broken: {}. " "Please check the URL and try again.\n\n" "Nevertheless, you can still browse the content " - "without origin information: %s" - % (gen_link(origin_url), gen_link(raw_cnt_url)) + "without origin information: {}", + gen_link(origin_url), + gen_link(raw_cnt_url), ) raise NotFoundExc(error_message) else: diff --git a/swh/web/browse/views/directory.py b/swh/web/browse/views/directory.py index b2cf685db..bc28ca954 100644 --- a/swh/web/browse/views/directory.py +++ b/swh/web/browse/views/directory.py @@ -8,6 +8,7 @@ from typing import Any, Dict, Optional from django.http import HttpRequest, HttpResponse from django.shortcuts import redirect, render +from django.utils.html import format_html from swh.model.swhids import ObjectType from swh.web.browse.browseurls import browse_route @@ -77,14 +78,15 @@ def _directory_browse( raw_dir_url = reverse( "browse-directory", url_args={"sha1_git": dir_sha1_git} ) - error_message = ( + error_message = format_html( "The Software Heritage archive has a directory " "with the hash you provided but the origin " - "mentioned in your request appears broken: %s. " + "mentioned in your request appears broken: {}. " "Please check the URL and try again.\n\n" "Nevertheless, you can still browse the directory " - "without origin information: %s" - % (gen_link(origin_url), gen_link(raw_dir_url)) + "without origin information: {}", + gen_link(origin_url), + gen_link(raw_dir_url), ) raise NotFoundExc(error_message) else: diff --git a/swh/web/browse/views/release.py b/swh/web/browse/views/release.py index 94e773440..47a659512 100644 --- a/swh/web/browse/views/release.py +++ b/swh/web/browse/views/release.py @@ -7,6 +7,7 @@ from typing import Optional from django.http import HttpRequest, HttpResponse from django.shortcuts import render +from django.utils.html import format_html from swh.model.swhids import ObjectType from swh.web.browse.browseurls import browse_route @@ -60,14 +61,15 @@ def release_browse(request: HttpRequest, sha1_git: str) -> HttpResponse: ) except NotFoundExc as e: raw_rel_url = reverse("browse-release", url_args={"sha1_git": sha1_git}) - error_message = ( + error_message = format_html( "The Software Heritage archive has a release " "with the hash you provided but the origin " - "mentioned in your request appears broken: %s. " + "mentioned in your request appears broken: {}. " "Please check the URL and try again.\n\n" "Nevertheless, you can still browse the release " - "without origin information: %s" - % (gen_link(origin_url), gen_link(raw_rel_url)) + "without origin information: {}", + gen_link(origin_url), + gen_link(raw_rel_url), ) if str(e).startswith("Origin"): raise NotFoundExc(error_message) diff --git a/swh/web/browse/views/revision.py b/swh/web/browse/views/revision.py index ea9184c08..fc85e4a8a 100644 --- a/swh/web/browse/views/revision.py +++ b/swh/web/browse/views/revision.py @@ -10,6 +10,7 @@ from typing import Any, Dict, List, Optional from django.http import HttpRequest, HttpResponse, JsonResponse from django.shortcuts import render +from django.utils.html import format_html from django.utils.safestring import mark_safe from swh.model.hashutil import hash_to_bytes @@ -352,14 +353,15 @@ def revision_browse(request: HttpRequest, sha1_git: str) -> HttpResponse: ) except NotFoundExc as e: raw_rev_url = reverse("browse-revision", url_args={"sha1_git": sha1_git}) - error_message = ( + error_message = format_html( "The Software Heritage archive has a revision " "with the hash you provided but the origin " - "mentioned in your request appears broken: %s. " + "mentioned in your request appears broken: {}. " "Please check the URL and try again.\n\n" "Nevertheless, you can still browse the revision " - "without origin information: %s" - % (gen_link(origin_url), gen_link(raw_rev_url)) + "without origin information: {}", + gen_link(origin_url), + gen_link(raw_rev_url), ) if str(e).startswith("Origin"): raise NotFoundExc(error_message) diff --git a/swh/web/utils/exc.py b/swh/web/utils/exc.py index df742da4a..bbfed8845 100644 --- a/swh/web/utils/exc.py +++ b/swh/web/utils/exc.py @@ -12,7 +12,7 @@ import sentry_sdk from django.core import exceptions from django.http import HttpRequest, HttpResponse from django.shortcuts import render -from django.utils.html import conditional_escape, escape +from django.utils.html import escape, format_html from rest_framework.exceptions import APIException from rest_framework.renderers import JSONRenderer @@ -41,7 +41,8 @@ class NotFoundExc(exceptions.ObjectDoesNotExist): """ - pass + def __str__(self): + return self.args[0] class ForbiddenExc(exceptions.PermissionDenied): @@ -188,11 +189,14 @@ def handle_view_exception(request: HttpRequest, exc: Exception) -> HttpResponse: was raised inside a swh-web browse view. """ sentry_capture_exception(exc) - error_code = 500 - error_description = "%s: %s" % (type(exc).__name__, str(exc)) + + error_description = format_html("{}: {}", type(exc).__name__, str(exc)) if get_config()["debug"]: - error_description = traceback.format_exc() - logger.error(error_description) + traceback_str = traceback.format_exc() + logger.error(traceback_str) + error_description = escape(traceback_str) + + error_code = 500 if isinstance(exc, BadInputExc): error_code = 400 if isinstance(exc, ForbiddenExc): @@ -200,8 +204,6 @@ def handle_view_exception(request: HttpRequest, exc: Exception) -> HttpResponse: if isinstance(exc, NotFoundExc): error_code = 404 - # some NotFoundExc texts have HTML links we want to preserve - error_description = conditional_escape(error_description) resp = _generate_error_page(request, error_code, error_description) if get_config()["debug"]: resp.traceback = error_description # type: ignore[attr-defined] -- GitLab