diff --git a/backend/src/data/Phisherman.ts b/backend/src/data/Phisherman.ts index 25fb59ce..44fe3fa5 100644 --- a/backend/src/data/Phisherman.ts +++ b/backend/src/data/Phisherman.ts @@ -53,11 +53,20 @@ function getKeyCacheRepository(): Repository { } 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( return pendingApiRequests.get(key)! as Promise; } - const requestPromise = (async () => { + let requestPromise = (async () => { const response = await fetch(url, { method, headers: new Headers({ @@ -104,11 +113,11 @@ async function apiCall( }); 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); diff --git a/backend/src/plugins/Phisherman/PhishermanPlugin.ts b/backend/src/plugins/Phisherman/PhishermanPlugin.ts index 50a6c321..4b8a9d5e 100644 --- a/backend/src/plugins/Phisherman/PhishermanPlugin.ts +++ b/backend/src/plugins/Phisherman/PhishermanPlugin.ts @@ -37,13 +37,20 @@ export const PhishermanPlugin = zeppelinGuildPlugin()({ if (!hasPhishermanMasterAPIKey()) { // 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; } const apiKey = pluginData.config.get().api_key; - if (apiKey && (await phishermanApiKeyIsValid(apiKey).catch(() => false))) { - pluginData.state.validApiKey = apiKey; + if (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; + } } }, });