Add lint rule for unhandled promise rejections
This commit is contained in:
1 parent
ad682d0b69
commit
0b60406b43
14 files changed
+177
-117
No files matched your search
@@ -71,7 +71,7 @@ let fulfillTimeout = null;
|
||||
function fulfill(): void {
|
||||
clearTimeout(fulfillTimeout);
|
||||
fulfillTimeout = setTimeout(() => {
|
||||
store.fulfill(lastRenderTimestamp, pageVisitTimestamp).then((updated) => {
|
||||
store.fulfill(lastRenderTimestamp, pageVisitTimestamp, (updated) => {
|
||||
if (updated) m.redraw();
|
||||
});
|
||||
}, 100);
|
||||
|
||||
+20
-15
@@ -27,23 +27,28 @@ const component: ClosureComponent = (vn): Component => {
|
||||
let interval: ReturnType<typeof setInterval>;
|
||||
let host: string;
|
||||
|
||||
const refresh = async (): Promise<void> => {
|
||||
let status = "";
|
||||
if (host) {
|
||||
try {
|
||||
const res = await store.ping(host);
|
||||
if (res["avg"] != null) status = `${Math.trunc(res["avg"])} ms`;
|
||||
else status = "Unreachable";
|
||||
} catch (err) {
|
||||
setTimeout(() => {
|
||||
throw err;
|
||||
}, 0);
|
||||
}
|
||||
const refresh = (): void => {
|
||||
if (!host) {
|
||||
const dom = (vn as VnodeDOM).dom;
|
||||
if (dom) dom.innerHTML = "";
|
||||
return;
|
||||
}
|
||||
|
||||
const dom = (vn as VnodeDOM).dom;
|
||||
|
||||
if (dom) dom.innerHTML = `Pinging ${host}: ${status}`;
|
||||
let status = "";
|
||||
store
|
||||
.ping(host)
|
||||
.then((res) => {
|
||||
if (res["avg"] != null) status = `${Math.trunc(res["avg"])} ms`;
|
||||
else status = "Unreachable";
|
||||
})
|
||||
.catch(() => {
|
||||
status = "Error!";
|
||||
clearInterval(interval);
|
||||
})
|
||||
.finally(() => {
|
||||
const dom = (vn as VnodeDOM).dom;
|
||||
if (dom) dom.innerHTML = `Pinging ${host}: ${status}`;
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
|
||||
@@ -79,6 +79,9 @@ const component: ClosureComponent = (): Component => {
|
||||
.then(() => {
|
||||
e.target.disabled = false;
|
||||
store.fulfill(0, Date.now());
|
||||
})
|
||||
.catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
},
|
||||
},
|
||||
|
||||
@@ -441,6 +441,9 @@ const component: ClosureComponent = (): Component => {
|
||||
)
|
||||
.then(() => {
|
||||
store.fulfill(0, Date.now());
|
||||
})
|
||||
.catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
},
|
||||
},
|
||||
|
||||
+45
-13
@@ -152,6 +152,8 @@ function checkConnection(): void {
|
||||
}
|
||||
}
|
||||
},
|
||||
}).catch((err) => {
|
||||
notifications.push("error", err.message);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -169,11 +171,25 @@ export async function xhrRequest(
|
||||
): any => {
|
||||
if (typeof extract === "function") return extract(xhr, _options);
|
||||
|
||||
// https://mithril.js.org/request.html#error-handling
|
||||
if (xhr.status !== 304 && Math.floor(xhr.status / 100) !== 2) {
|
||||
const err = new Error();
|
||||
err["message"] =
|
||||
xhr.status === 0
|
||||
? "Server is unrachable"
|
||||
: `Unexpected response status code ${xhr.status}`;
|
||||
err["code"] = xhr.status;
|
||||
err["response"] = xhr.responseText;
|
||||
throw err;
|
||||
}
|
||||
|
||||
let response: any;
|
||||
if (typeof deserialize === "function") {
|
||||
response = deserialize(xhr.responseText);
|
||||
} else if (
|
||||
xhr.getResponseHeader("content-type").startsWith("application/json")
|
||||
(xhr.getResponseHeader("content-type") || "").startsWith(
|
||||
"application/json"
|
||||
)
|
||||
) {
|
||||
try {
|
||||
response = xhr.responseText ? JSON.parse(xhr.responseText) : null;
|
||||
@@ -184,15 +200,6 @@ export async function xhrRequest(
|
||||
response = xhr.responseText;
|
||||
}
|
||||
|
||||
// https://mithril.js.org/request.html#error-handling
|
||||
if (xhr.status !== 304 && Math.floor(xhr.status / 100) !== 2) {
|
||||
const err = new Error();
|
||||
err["message"] = xhr.responseText;
|
||||
err["code"] = xhr.status;
|
||||
err["response"] = response;
|
||||
throw err;
|
||||
}
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
@@ -340,7 +347,7 @@ export function fetch(
|
||||
return queryResponse;
|
||||
}
|
||||
|
||||
export function fulfill(
|
||||
function _fulfill(
|
||||
accessTimestamp: number,
|
||||
_fulfillTimestamp: number
|
||||
): Promise<boolean> {
|
||||
@@ -377,7 +384,16 @@ export function fulfill(
|
||||
m.buildQueryString({
|
||||
filter: memoizedStringify(filter),
|
||||
}),
|
||||
extract: (xhr) => +xhr.getResponseHeader("x-total-count"),
|
||||
extract: (xhr) => {
|
||||
if (!xhr.status) {
|
||||
throw new Error("Server is unrachable");
|
||||
} else if (xhr.status !== 200) {
|
||||
throw new Error(
|
||||
`Unexpected response status code ${xhr.status}`
|
||||
);
|
||||
}
|
||||
return +xhr.getResponseHeader("x-total-count");
|
||||
},
|
||||
background: true,
|
||||
})
|
||||
.then((c) => {
|
||||
@@ -560,6 +576,17 @@ export function fulfill(
|
||||
});
|
||||
}
|
||||
|
||||
export function fulfill(
|
||||
accessTimestamp: number,
|
||||
_fulfillTimestamp: number,
|
||||
callback?: (updated: boolean) => void
|
||||
): void {
|
||||
_fulfill(accessTimestamp, _fulfillTimestamp).then(callback, (err) => {
|
||||
notifications.push("error", err.message);
|
||||
if (callback) callback(false);
|
||||
});
|
||||
}
|
||||
|
||||
export function getTimestamp(): number {
|
||||
return fulfillTimestamp;
|
||||
}
|
||||
@@ -647,7 +674,12 @@ export function resourceExists(resource: string, id: string): Promise<number> {
|
||||
m.buildQueryString({
|
||||
filter: memoizedStringify(filter),
|
||||
}),
|
||||
extract: (xhr) => +xhr.getResponseHeader("x-total-count"),
|
||||
extract: (xhr) => {
|
||||
if (!xhr.status) throw new Error("Server is unrachable");
|
||||
else if (xhr.status !== 200)
|
||||
throw new Error(`Unexpected response status code ${xhr.status}`);
|
||||
return +xhr.getResponseHeader("x-total-count");
|
||||
},
|
||||
background: true,
|
||||
});
|
||||
}
|
||||
|
||||
+17
-9
@@ -87,16 +87,24 @@ export const component: ClosureComponent = (vnode): Component => {
|
||||
})
|
||||
.then(() => {
|
||||
setTimeout(() => {
|
||||
m.request({ url: "init" }).then((o) => {
|
||||
e.target.disabled = false;
|
||||
options = o;
|
||||
notifications.push("success", "Initialization complete", {
|
||||
"Open Sesame!": () => {
|
||||
m.route.set("/login");
|
||||
window.location.reload();
|
||||
},
|
||||
m.request({ url: "init" })
|
||||
.then((o) => {
|
||||
e.target.disabled = false;
|
||||
options = o;
|
||||
notifications.push(
|
||||
"success",
|
||||
"Initialization complete",
|
||||
{
|
||||
"Open Sesame!": () => {
|
||||
m.route.set("/login");
|
||||
window.location.reload();
|
||||
},
|
||||
}
|
||||
);
|
||||
})
|
||||
.catch((err) => {
|
||||
notifications.push("error", err.message);
|
||||
});
|
||||
});
|
||||
}, 3000);
|
||||
if (opts["users"]) {
|
||||
alert(
|
||||
|
||||
Reference in new issue
Block a user