3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-16 22:21:51 +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 { class PhishermanApiError extends Error {
method: string;
url: string;
status: number; status: number;
constructor(status: number, message: string) {
constructor(method: string, url: string, status: number, message: string) {
super(message); super(message);
this.method = method;
this.url = url;
this.status = status; this.status = status;
} }
toString() {
return `Error ${this.status} in ${this.method} ${this.url}: ${this.message}`;
}
} }
export function hasPhishermanMasterAPIKey() { export function hasPhishermanMasterAPIKey() {
@ -92,7 +101,7 @@ async function apiCall<T>(
return pendingApiRequests.get(key)! as Promise<T>; return pendingApiRequests.get(key)! as Promise<T>;
} }
const requestPromise = (async () => { let requestPromise = (async () => {
const response = await fetch(url, { const response = await fetch(url, {
method, method,
headers: new Headers({ headers: new Headers({
@ -104,11 +113,11 @@ async function apiCall<T>(
}); });
const data = await response.json().catch(() => null); const data = await response.json().catch(() => null);
if (!response.ok || (data as any)?.success === false) { 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; return data;
})(); })();
requestPromise.finally(() => { requestPromise = requestPromise.finally(() => {
pendingApiRequests.delete(key); pendingApiRequests.delete(key);
}); });
pendingApiRequests.set(key, requestPromise); pendingApiRequests.set(key, requestPromise);

View file

@ -37,13 +37,20 @@ export const PhishermanPlugin = zeppelinGuildPlugin<PhishermanPluginType>()({
if (!hasPhishermanMasterAPIKey()) { if (!hasPhishermanMasterAPIKey()) {
// tslint:disable-next-line:no-console // tslint:disable-next-line:no-console
console.warn("Could not load Phisherman plugin: master API key is missing"); console.warn("[PHISHERMAN] Could not load Phisherman plugin: master API key is missing");
return; return;
} }
const apiKey = pluginData.config.get().api_key; const apiKey = pluginData.config.get().api_key;
if (apiKey && (await phishermanApiKeyIsValid(apiKey).catch(() => false))) { if (apiKey) {
pluginData.state.validApiKey = apiKey; const isValid = await phishermanApiKeyIsValid(apiKey).catch((err) => {
// tslint:disable-next-line:no-console
console.warn(`[PHISHERMAN] Error checking user API key validity:\n${err.toString()}`);
return false;
});
if (isValid) {
pluginData.state.validApiKey = apiKey;
}
} }
}, },
}); });