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();
// 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);

View File

@ -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}`);
}
}
}

View File

@ -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));
});