fix: error handling for declaration data

main
Xubai Wang 2022-03-14 10:59:25 +08:00
parent 5e2e30c4f6
commit 0ec492ba98
3 changed files with 53 additions and 35 deletions

View File

@ -56,7 +56,7 @@ export class DeclarationDataCenter {
const timestamp = await timestampRes.text(); const timestamp = await timestampRes.text();
// try to use cache first // try to use cache first
const data = await fetchCachedDeclarationData(timestamp); const data = await fetchCachedDeclarationData(timestamp).catch(_e => null);
if (data) { if (data) {
// if data is defined, use the cached one. // if data is defined, use the cached one.
DeclarationDataCenter.singleton = new DeclarationDataCenter(data); DeclarationDataCenter.singleton = new DeclarationDataCenter(data);

View File

@ -53,6 +53,7 @@ async function findAndRedirect(pattern, strict, view) {
window.location.replace(`${SITE_ROOT}404.html`); window.location.replace(`${SITE_ROOT}404.html`);
} }
// search for result // search for result
try {
const dataCenter = await DeclarationDataCenter.init(); const dataCenter = await DeclarationDataCenter.init();
let result = (dataCenter.search(pattern, strict) ?? [])[0]; // in case return non array let result = (dataCenter.search(pattern, strict) ?? [])[0]; // in case return non array
// if no result found, redirect to the 404 page // if no result found, redirect to the 404 page
@ -72,4 +73,7 @@ async function findAndRedirect(pattern, strict, view) {
window.location.replace(result.docLink); window.location.replace(result.docLink);
} }
} }
} catch (e) {
document.write(`Cannot fetch data, please check your network connection.\n${e}`);
}
} }

View File

@ -4,7 +4,6 @@
import { DeclarationDataCenter } from "./declaration-data.js"; import { DeclarationDataCenter } from "./declaration-data.js";
const SEARCH_FORM = document.querySelector("#search_form"); const SEARCH_FORM = document.querySelector("#search_form");
const SEARCH_INPUT = SEARCH_FORM.querySelector("input[name=q]"); const SEARCH_INPUT = SEARCH_FORM.querySelector("input[name=q]");
@ -34,9 +33,7 @@ function handleSearchCursorUpDown(down) {
* Perform search (when enter is pressed). * Perform search (when enter is pressed).
*/ */
function handleSearchEnter() { function handleSearchEnter() {
const sel = const sel = sr.querySelector(`.selected`) || sr.firstChild;
sr.querySelector(`.selected`) ||
sr.firstChild;
sel.click(); 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; const text = ev.target.value;
// If no input clear all. // If no input clear all.
@ -86,7 +83,8 @@ SEARCH_INPUT.addEventListener("input", async (ev) => {
// searching // searching
sr.setAttribute("state", "loading"); sr.setAttribute("state", "loading");
const dataCenter = await DeclarationDataCenter.init();
if (dataCenter) {
const result = dataCenter.search(text, false); const result = dataCenter.search(text, false);
// in case user has updated the input. // in case user has updated the input.
@ -100,5 +98,21 @@ SEARCH_INPUT.addEventListener("input", async (ev) => {
d.title = name; d.title = name;
d.href = docLink; d.href = docLink;
} }
}
// handle error
else {
removeAllChildren(sr);
const d = sr.appendChild(document.createElement("a"));
d.innerText = `Cannot fetch data, please check your network connection.\n${err}`;
}
sr.setAttribute("state", "done"); 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));
});