From 0ec492ba98759f629c8e44c35fa8196489d3b977 Mon Sep 17 00:00:00 2001 From: Xubai Wang Date: Mon, 14 Mar 2022 10:59:25 +0800 Subject: [PATCH] fix: error handling for declaration data --- static/declaration-data.js | 2 +- static/find/find.js | 38 ++++++++++++++++-------------- static/search.js | 48 ++++++++++++++++++++++++-------------- 3 files changed, 53 insertions(+), 35 deletions(-) diff --git a/static/declaration-data.js b/static/declaration-data.js index cb7a90c..2b778d5 100644 --- a/static/declaration-data.js +++ b/static/declaration-data.js @@ -56,7 +56,7 @@ export class DeclarationDataCenter { const timestamp = await timestampRes.text(); // try to use cache first - const data = await fetchCachedDeclarationData(timestamp); + const data = await fetchCachedDeclarationData(timestamp).catch(_e => null); if (data) { // if data is defined, use the cached one. DeclarationDataCenter.singleton = new DeclarationDataCenter(data); diff --git a/static/find/find.js b/static/find/find.js index 4b65f26..fbb9ca7 100644 --- a/static/find/find.js +++ b/static/find/find.js @@ -53,23 +53,27 @@ async function findAndRedirect(pattern, strict, view) { window.location.replace(`${SITE_ROOT}404.html`); } // search for result - const dataCenter = await DeclarationDataCenter.init(); - let result = (dataCenter.search(pattern, strict) ?? [])[0]; // in case return non array - // if no result found, redirect to the 404 page - if (!result) { - // TODO: better url semantic for 404, current implementation will lead to duplicate search for fuzzy match if not found. - window.location.replace(`${SITE_ROOT}404.html#${pattern ?? ""}`); - } else { - // success, redirect to doc or source page, or to the semantic rdf. - if (!view) { - window.location.replace(result.link); - } else if (view == "doc") { - window.location.replace(result.docLink); - } else if (view == "src") { - window.location.replace(result.sourceLink); + try { + const dataCenter = await DeclarationDataCenter.init(); + let result = (dataCenter.search(pattern, strict) ?? [])[0]; // in case return non array + // if no result found, redirect to the 404 page + if (!result) { + // TODO: better url semantic for 404, current implementation will lead to duplicate search for fuzzy match if not found. + window.location.replace(`${SITE_ROOT}404.html#${pattern ?? ""}`); } else { - // fallback to doc page - window.location.replace(result.docLink); + // success, redirect to doc or source page, or to the semantic rdf. + if (!view) { + window.location.replace(result.link); + } else if (view == "doc") { + window.location.replace(result.docLink); + } else if (view == "src") { + window.location.replace(result.sourceLink); + } else { + // fallback to doc page + window.location.replace(result.docLink); + } } + } catch (e) { + document.write(`Cannot fetch data, please check your network connection.\n${e}`); } -} \ No newline at end of file +} diff --git a/static/search.js b/static/search.js index 1df8690..5b075de 100644 --- a/static/search.js +++ b/static/search.js @@ -4,7 +4,6 @@ import { DeclarationDataCenter } from "./declaration-data.js"; - const SEARCH_FORM = document.querySelector("#search_form"); const SEARCH_INPUT = SEARCH_FORM.querySelector("input[name=q]"); @@ -34,9 +33,7 @@ function handleSearchCursorUpDown(down) { * Perform search (when enter is pressed). */ function handleSearchEnter() { - const sel = - sr.querySelector(`.selected`) || - sr.firstChild; + const sel = sr.querySelector(`.selected`) || sr.firstChild; sel.click(); } @@ -72,9 +69,9 @@ function removeAllChildren(node) { } /** - * Search autocompletion. + * Handle user input and perform search. */ -SEARCH_INPUT.addEventListener("input", async (ev) => { +function handleSearch(dataCenter, err, ev) { const text = ev.target.value; // If no input clear all. @@ -86,19 +83,36 @@ SEARCH_INPUT.addEventListener("input", async (ev) => { // searching sr.setAttribute("state", "loading"); - const dataCenter = await DeclarationDataCenter.init(); - const result = dataCenter.search(text, false); - // in case user has updated the input. - if (ev.target.value != text) return; + if (dataCenter) { + const result = dataCenter.search(text, false); - // update search results - removeAllChildren(sr); - for (const { name, docLink } of result) { + // in case user has updated the input. + if (ev.target.value != text) return; + + // update search results + removeAllChildren(sr); + for (const { name, docLink } of result) { + const d = sr.appendChild(document.createElement("a")); + d.innerText = name; + d.title = name; + d.href = docLink; + } + } + // handle error + else { + removeAllChildren(sr); const d = sr.appendChild(document.createElement("a")); - d.innerText = name; - d.title = name; - d.href = docLink; + d.innerText = `Cannot fetch data, please check your network connection.\n${err}`; } sr.setAttribute("state", "done"); -}); +} + +DeclarationDataCenter.init() + .then((dataCenter) => { + // Search autocompletion. + SEARCH_INPUT.addEventListener("input", ev => handleSearch(dataCenter, null, ev)); + }) + .catch(e => { + SEARCH_INPUT.addEventListener("input", ev => handleSearch(null, e, ev)); + });