From 51b90130cf2864898ab4b139dd401d4de4041170 Mon Sep 17 00:00:00 2001 From: jaclin Date: Fri, 17 Apr 2026 21:43:54 -0400 Subject: [PATCH] Fix: check HTTP status in history.js fetch calls (#6282) --- app/assets/javascripts/index/history.js | 143 ++++++++++++++++++++---- 1 file changed, 124 insertions(+), 19 deletions(-) diff --git a/app/assets/javascripts/index/history.js b/app/assets/javascripts/index/history.js index 8b64a4432cb..905ed21b46a 100644 --- a/app/assets/javascripts/index/history.js +++ b/app/assets/javascripts/index/history.js @@ -32,6 +32,83 @@ OSM.History = function (map) { let changesetIntersectionObserver; + function safeHistoryT(key, options) { + try { + return OSM.i18n.t(key, options); + } catch (e) { + return key; + } + } + + function buildFetchErrorMessage(response) { + const status = response?.status; + const statusText = response?.statusText; + const parts = []; + if (status) parts.push(status); + if (statusText) parts.push(statusText); + return parts.join(" ") || safeHistoryT("javascripts.history.load_failed_unknown"); + } + + function fetchHtmlOrThrow(url) { + return fetch(url).then(response => { + if (response.ok) return response.text(); + throw new Error(buildFetchErrorMessage(response)); + }); + } + + function removeHistoryLoadError() { + $("#sidebar_content .history-load-error").remove(); + } + + function showHistoryLoadError(options) { + const { message, retry } = options; + const detail = String(message ?? ""); + // Always resolve targets from the live DOM (Turbo may replace sidebar HTML after fetch starts). + const $changesets = $("#sidebar_content .changesets"); + const $target = $changesets.length ? $changesets : $("#sidebar_content"); + + removeHistoryLoadError(); + + try { + const $alert = $("
") + .attr("role", "alert") + .append( + $("
").append( + $("
").append( + $("
").text(safeHistoryT("javascripts.history.load_failed_title")), + $("
").text(safeHistoryT("javascripts.history.load_failed_body", { message: detail })) + ), + $("