3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-15 05:41: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 {
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);

View file

@ -37,13 +37,20 @@ export const PhishermanPlugin = zeppelinGuildPlugin<PhishermanPluginType>()({
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;
}
}
},
});