From 09a7eabda137e77f81ab31f65d69329670693c8d Mon Sep 17 00:00:00 2001 From: Johann150 Date: Thu, 4 Aug 2022 11:00:02 +0200 Subject: [PATCH] backend: fix lint "no-throw-literal" --- packages/backend/src/boot/master.ts | 7 +++---- packages/backend/src/config/load.ts | 2 +- packages/backend/src/misc/captcha.ts | 12 ++++++------ packages/backend/src/misc/gen-id.ts | 2 +- packages/backend/src/prelude/time.ts | 2 +- .../src/queue/processors/db/import-blocking.ts | 2 +- .../src/queue/processors/db/import-following.ts | 2 +- .../backend/src/queue/processors/db/import-muting.ts | 2 +- packages/backend/src/queue/processors/deliver.ts | 2 +- packages/backend/src/queue/processors/inbox.ts | 2 +- .../backend/src/queue/processors/webhook-deliver.ts | 2 +- .../src/remote/activitypub/misc/ld-signature.ts | 4 ++-- .../backend/src/remote/activitypub/models/note.ts | 4 ++-- .../src/server/api/endpoints/admin/relays/add.ts | 3 ++- .../src/server/api/endpoints/admin/suspend-user.ts | 2 +- .../src/server/api/endpoints/notes/search-by-tag.ts | 6 +++--- .../backend/src/server/api/endpoints/reset-db.ts | 2 +- .../backend/src/services/fetch-instance-metadata.ts | 12 ++++++------ packages/backend/src/services/relay.ts | 2 +- 19 files changed, 36 insertions(+), 36 deletions(-) diff --git a/packages/backend/src/boot/master.ts b/packages/backend/src/boot/master.ts index 1093e6edc..8583823ee 100644 --- a/packages/backend/src/boot/master.ts +++ b/packages/backend/src/boot/master.ts @@ -110,13 +110,12 @@ function loadConfigBoot(): Config { try { config = loadConfig(); } catch (exception) { - if (typeof exception === 'string') { - configLogger.error(exception); - process.exit(1); - } if (exception.code === 'ENOENT') { configLogger.error('Configuration file not found', null, true); process.exit(1); + } else if (e instanceof Error) { + configLogger.error(e.message); + process.exit(1); } throw exception; } diff --git a/packages/backend/src/config/load.ts b/packages/backend/src/config/load.ts index d82c8a396..535f258df 100644 --- a/packages/backend/src/config/load.ts +++ b/packages/backend/src/config/load.ts @@ -59,6 +59,6 @@ function tryCreateUrl(url: string) { try { return new URL(url); } catch (e) { - throw `url="${url}" is not a valid URL.`; + throw new Error(`url="${url}" is not a valid URL.`); } } diff --git a/packages/backend/src/misc/captcha.ts b/packages/backend/src/misc/captcha.ts index 2a128c25f..4238116fe 100644 --- a/packages/backend/src/misc/captcha.ts +++ b/packages/backend/src/misc/captcha.ts @@ -5,23 +5,23 @@ import { getAgentByUrl } from './fetch.js'; export async function verifyRecaptcha(secret: string, response: string) { const result = await getCaptchaResponse('https://www.recaptcha.net/recaptcha/api/siteverify', secret, response).catch(e => { - throw `recaptcha-request-failed: ${e}`; + throw new Error(`recaptcha-request-failed: ${e.message}`); }); if (result.success !== true) { const errorCodes = result['error-codes'] ? result['error-codes']?.join(', ') : ''; - throw `recaptcha-failed: ${errorCodes}`; + throw new Error(`recaptcha-failed: ${errorCodes}`); } } export async function verifyHcaptcha(secret: string, response: string) { const result = await getCaptchaResponse('https://hcaptcha.com/siteverify', secret, response).catch(e => { - throw `hcaptcha-request-failed: ${e}`; + throw new Error(`hcaptcha-request-failed: ${e.message}`); }); if (result.success !== true) { const errorCodes = result['error-codes'] ? result['error-codes']?.join(', ') : ''; - throw `hcaptcha-failed: ${errorCodes}`; + throw new Error(`hcaptcha-failed: ${errorCodes}`); } } @@ -46,11 +46,11 @@ async function getCaptchaResponse(url: string, secret: string, response: string) //timeout: 10 * 1000, agent: getAgentByUrl, }).catch(e => { - throw `${e.message || e}`; + throw new Error(`${e.message || e}`); }); if (!res.ok) { - throw `${res.status}`; + throw new Error(`${res.status}`); } return await res.json() as CaptchaResponse; diff --git a/packages/backend/src/misc/gen-id.ts b/packages/backend/src/misc/gen-id.ts index 1a7a511b6..3caa81906 100644 --- a/packages/backend/src/misc/gen-id.ts +++ b/packages/backend/src/misc/gen-id.ts @@ -13,7 +13,7 @@ export function genId(date?: Date): string { let t = date.getTime(); t -= TIME2000; if (t < 0) t = 0; - if (isNaN(t)) throw 'Failed to create AID: Invalid Date'; + if (isNaN(t)) throw new Error('Failed to create AID: Invalid Date'); const time = t.toString(36).padStart(8, '0'); counter++; diff --git a/packages/backend/src/prelude/time.ts b/packages/backend/src/prelude/time.ts index 34e8b6b17..0da1f7913 100644 --- a/packages/backend/src/prelude/time.ts +++ b/packages/backend/src/prelude/time.ts @@ -13,7 +13,7 @@ export function dateUTC(time: number[]): Date { : time.length === 7 ? Date.UTC(time[0], time[1], time[2], time[3], time[4], time[5], time[6]) : null; - if (!d) throw 'wrong number of arguments'; + if (!d) throw new Error('wrong number of arguments'); return new Date(d); } diff --git a/packages/backend/src/queue/processors/db/import-blocking.ts b/packages/backend/src/queue/processors/db/import-blocking.ts index ec6061aba..33bf45730 100644 --- a/packages/backend/src/queue/processors/db/import-blocking.ts +++ b/packages/backend/src/queue/processors/db/import-blocking.ts @@ -55,7 +55,7 @@ export async function importBlocking(job: Bull.Job, done: a } if (target == null) { - throw `cannot resolve user: @${username}@${host}`; + throw new Error(`cannot resolve user: @${username}@${host}`); } // skip myself diff --git a/packages/backend/src/queue/processors/db/import-following.ts b/packages/backend/src/queue/processors/db/import-following.ts index a19612389..e01ffe517 100644 --- a/packages/backend/src/queue/processors/db/import-following.ts +++ b/packages/backend/src/queue/processors/db/import-following.ts @@ -55,7 +55,7 @@ export async function importFollowing(job: Bull.Job, done: } if (target == null) { - throw `cannot resolve user: @${username}@${host}`; + throw new Error(`cannot resolve user: @${username}@${host}`); } // skip myself diff --git a/packages/backend/src/queue/processors/db/import-muting.ts b/packages/backend/src/queue/processors/db/import-muting.ts index b0c69829b..ec7c6a8d7 100644 --- a/packages/backend/src/queue/processors/db/import-muting.ts +++ b/packages/backend/src/queue/processors/db/import-muting.ts @@ -56,7 +56,7 @@ export async function importMuting(job: Bull.Job, done: any } if (target == null) { - throw `cannot resolve user: @${username}@${host}`; + throw new Error(`cannot resolve user: @${username}@${host}`); } // skip myself diff --git a/packages/backend/src/queue/processors/deliver.ts b/packages/backend/src/queue/processors/deliver.ts index a9229a4bc..a7a0e4be9 100644 --- a/packages/backend/src/queue/processors/deliver.ts +++ b/packages/backend/src/queue/processors/deliver.ts @@ -89,7 +89,7 @@ export default async (job: Bull.Job) => { } // 5xx etc. - throw `${res.statusCode} ${res.statusMessage}`; + throw new Error(`${res.statusCode} ${res.statusMessage}`); } else { // DNS error, socket error, timeout ... throw res; diff --git a/packages/backend/src/queue/processors/inbox.ts b/packages/backend/src/queue/processors/inbox.ts index f48c5e266..bf25aca20 100644 --- a/packages/backend/src/queue/processors/inbox.ts +++ b/packages/backend/src/queue/processors/inbox.ts @@ -62,7 +62,7 @@ export default async (job: Bull.Job): Promise => { if (e.isClientError) { return `skip: Ignored deleted actors on both ends ${activity.actor} - ${e.statusCode}`; } - throw `Error in actor ${activity.actor} - ${e.statusCode || e}`; + throw new Error(`Error in actor ${activity.actor} - ${e.statusCode || e}`); } } } diff --git a/packages/backend/src/queue/processors/webhook-deliver.ts b/packages/backend/src/queue/processors/webhook-deliver.ts index 64396b458..07dfb61c6 100644 --- a/packages/backend/src/queue/processors/webhook-deliver.ts +++ b/packages/backend/src/queue/processors/webhook-deliver.ts @@ -49,7 +49,7 @@ export default async (job: Bull.Job) => { } // 5xx etc. - throw `${res.statusCode} ${res.statusMessage}`; + throw new Error(`${res.statusCode} ${res.statusMessage}`); } else { // DNS error, socket error, timeout ... throw res; diff --git a/packages/backend/src/remote/activitypub/misc/ld-signature.ts b/packages/backend/src/remote/activitypub/misc/ld-signature.ts index 49a8dbbe9..4e0940a72 100644 --- a/packages/backend/src/remote/activitypub/misc/ld-signature.ts +++ b/packages/backend/src/remote/activitypub/misc/ld-signature.ts @@ -85,7 +85,7 @@ export class LdSignature { private getLoader() { return async (url: string): Promise => { - if (!url.match('^https?\:\/\/')) throw `Invalid URL ${url}`; + if (!url.match('^https?\:\/\/')) throw new Error(`Invalid URL ${url}`); if (this.preLoad) { if (url in CONTEXTS) { @@ -118,7 +118,7 @@ export class LdSignature { agent: u => u.protocol === 'http:' ? httpAgent : httpsAgent, }).then(res => { if (!res.ok) { - throw `${res.status} ${res.statusText}`; + throw new Error(`${res.status} ${res.statusText}`); } else { return res.json(); } diff --git a/packages/backend/src/remote/activitypub/models/note.ts b/packages/backend/src/remote/activitypub/models/note.ts index b1fa1a0cb..c532f81ae 100644 --- a/packages/backend/src/remote/activitypub/models/note.ts +++ b/packages/backend/src/remote/activitypub/models/note.ts @@ -189,7 +189,7 @@ export async function createNote(value: string | IObject, resolver?: Resolver, s quote = results.filter((x): x is { status: 'ok', res: Note | null } => x.status === 'ok').map(x => x.res).find(x => x); if (!quote) { if (results.some(x => x.status === 'temperror')) { - throw 'quote resolve failed'; + throw new Error('quote resolve failed'); } } } @@ -276,7 +276,7 @@ export async function resolveNote(value: string | IObject, resolver?: Resolver): // ブロックしてたら中断 const meta = await fetchMeta(); - if (meta.blockedHosts.includes(extractDbHost(uri))) throw { statusCode: 451 }; + if (meta.blockedHosts.includes(extractDbHost(uri))) throw new StatusError('host blocked', 451, `host ${extractDbHost(uri)} is blocked`); const unlock = await getApLock(uri); diff --git a/packages/backend/src/server/api/endpoints/admin/relays/add.ts b/packages/backend/src/server/api/endpoints/admin/relays/add.ts index caee34787..94f757bdc 100644 --- a/packages/backend/src/server/api/endpoints/admin/relays/add.ts +++ b/packages/backend/src/server/api/endpoints/admin/relays/add.ts @@ -27,6 +27,7 @@ export const meta = { format: 'id', }, inbox: { + description: 'URL of the inbox, must be a https scheme URL', type: 'string', optional: false, nullable: false, format: 'url', @@ -56,7 +57,7 @@ export const paramDef = { // eslint-disable-next-line import/no-default-export export default define(meta, paramDef, async (ps, user) => { try { - if (new URL(ps.inbox).protocol !== 'https:') throw 'https only'; + if (new URL(ps.inbox).protocol !== 'https:') throw new Error('https only'); } catch { throw new ApiError(meta.errors.invalidUrl); } diff --git a/packages/backend/src/server/api/endpoints/admin/suspend-user.ts b/packages/backend/src/server/api/endpoints/admin/suspend-user.ts index 8b62daf7a..90ad703a3 100644 --- a/packages/backend/src/server/api/endpoints/admin/suspend-user.ts +++ b/packages/backend/src/server/api/endpoints/admin/suspend-user.ts @@ -68,7 +68,7 @@ async function unFollowAll(follower: User) { }); if (followee == null) { - throw `Cant find followee ${following.followeeId}`; + throw new Error(`Cant find followee ${following.followeeId}`); } await deleteFollowing(follower, followee, true); diff --git a/packages/backend/src/server/api/endpoints/notes/search-by-tag.ts b/packages/backend/src/server/api/endpoints/notes/search-by-tag.ts index 777de7221..ef9f6c39c 100644 --- a/packages/backend/src/server/api/endpoints/notes/search-by-tag.ts +++ b/packages/backend/src/server/api/endpoints/notes/search-by-tag.ts @@ -86,14 +86,14 @@ export default define(meta, paramDef, async (ps, me) => { try { if (ps.tag) { - if (!safeForSql(ps.tag)) throw 'Injection'; + if (!safeForSql(ps.tag)) throw new Error('Injection'); query.andWhere(`'{"${normalizeForSearch(ps.tag)}"}' <@ note.tags`); } else { query.andWhere(new Brackets(qb => { for (const tags of ps.query!) { qb.orWhere(new Brackets(qb => { for (const tag of tags) { - if (!safeForSql(tag)) throw 'Injection'; + if (!safeForSql(tag)) throw new Error('Injection'); qb.andWhere(`'{"${normalizeForSearch(tag)}"}' <@ note.tags`); } })); @@ -101,7 +101,7 @@ export default define(meta, paramDef, async (ps, me) => { })); } } catch (e) { - if (e === 'Injection') return []; + if (e.message === 'Injection') return []; throw e; } diff --git a/packages/backend/src/server/api/endpoints/reset-db.ts b/packages/backend/src/server/api/endpoints/reset-db.ts index 02adfb540..7f17339e9 100644 --- a/packages/backend/src/server/api/endpoints/reset-db.ts +++ b/packages/backend/src/server/api/endpoints/reset-db.ts @@ -21,7 +21,7 @@ export const paramDef = { // eslint-disable-next-line import/no-default-export export default define(meta, paramDef, async (ps, user) => { - if (process.env.NODE_ENV !== 'test') throw 'NODE_ENV is not a test'; + if (process.env.NODE_ENV !== 'test') throw new Error('NODE_ENV is not a test'); await resetDb(); diff --git a/packages/backend/src/services/fetch-instance-metadata.ts b/packages/backend/src/services/fetch-instance-metadata.ts index cd8cc7f33..d318ed74c 100644 --- a/packages/backend/src/services/fetch-instance-metadata.ts +++ b/packages/backend/src/services/fetch-instance-metadata.ts @@ -94,14 +94,14 @@ async function fetchNodeinfo(instance: Instance): Promise { const wellknown = await getJson('https://' + instance.host + '/.well-known/nodeinfo') .catch(e => { if (e.statusCode === 404) { - throw 'No nodeinfo provided'; + throw new Error('No nodeinfo provided'); } else { - throw e.statusCode || e.message; + throw new Error(e.statusCode || e.message); } }) as Record; if (wellknown.links == null || !Array.isArray(wellknown.links)) { - throw 'No wellknown links'; + throw new Error('No wellknown links'); } const links = wellknown.links as any[]; @@ -112,19 +112,19 @@ async function fetchNodeinfo(instance: Instance): Promise { const link = lnik2_1 || lnik2_0 || lnik1_0; if (link == null) { - throw 'No nodeinfo link provided'; + throw new Error('No nodeinfo link provided'); } const info = await getJson(link.href) .catch(e => { - throw e.statusCode || e.message; + throw new Error(e.statusCode || e.message); }); logger.succ(`Successfuly fetched nodeinfo of ${instance.host}`); return info as NodeInfo; } catch (e) { - logger.error(`Failed to fetch nodeinfo of ${instance.host}: ${e}`); + logger.error(`Failed to fetch nodeinfo of ${instance.host}: ${e.message}`); throw e; } diff --git a/packages/backend/src/services/relay.ts b/packages/backend/src/services/relay.ts index 6bc430443..a05645f09 100644 --- a/packages/backend/src/services/relay.ts +++ b/packages/backend/src/services/relay.ts @@ -47,7 +47,7 @@ export async function removeRelay(inbox: string) { }); if (relay == null) { - throw 'relay not found'; + throw new Error('relay not found'); } const relayActor = await getRelayActor();