3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 12:25:02 +00:00

fix: fix not being able to catch Phisherman API call errors

This commit is contained in:
Dragory 2021-11-01 17:09:02 +02:00
parent b162d8c72e
commit 1081d1b361
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
2 changed files with 23 additions and 7 deletions

View file

@ -53,11 +53,20 @@ function getKeyCacheRepository(): Repository<PhishermanKeyCacheEntry> {
}
class PhishermanApiError extends Error {
method: string;
url: string;
status: number;
constructor(status: number, message: string) {
constructor(method: string, url: string, status: number, message: string) {
super(message);
this.method = method;
this.url = url;
this.status = status;
}
toString() {
return `Error ${this.status} in ${this.method} ${this.url}: ${this.message}`;
}
}
export function hasPhishermanMasterAPIKey() {
@ -92,7 +101,7 @@ async function apiCall<T>(
return pendingApiRequests.get(key)! as Promise<T>;
}
const requestPromise = (async () => {
let requestPromise = (async () => {
const response = await fetch(url, {
method,
headers: new Headers({
@ -104,11 +113,11 @@ async function apiCall<T>(
});
const data = await response.json().catch(() => null);
if (!response.ok || (data as any)?.success === false) {
throw new PhishermanApiError(response.status, (data as any)?.message ?? "");
throw new PhishermanApiError(method, url, response.status, (data as any)?.message ?? "");
}
return data;
})();
requestPromise.finally(() => {
requestPromise = requestPromise.finally(() => {
pendingApiRequests.delete(key);
});
pendingApiRequests.set(key, requestPromise);