From 49686050cc981904a3d52f80e8ccea95f94969e1 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/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 +- 18 files changed, 35 insertions(+), 35 deletions(-) diff --git a/packages/backend/src/boot/master.ts b/packages/backend/src/boot/master.ts index 94538ed1f..937333bc1 100644 --- a/packages/backend/src/boot/master.ts +++ b/packages/backend/src/boot/master.ts @@ -112,13 +112,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 9654a4f3b..0f3af08ef 100644 --- a/packages/backend/src/config/load.ts +++ b/packages/backend/src/config/load.ts @@ -57,6 +57,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 9a87a4a3c..947ab27e2 100644 --- a/packages/backend/src/misc/captcha.ts +++ b/packages/backend/src/misc/captcha.ts @@ -5,23 +5,23 @@ import config from '@/config/index.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/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 8bddf34bc..42a14fb3e 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 8ce2c367d..1824c3669 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 8552b797b..aa14ff526 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 01ba4381a..df070d1de 100644 --- a/packages/backend/src/queue/processors/deliver.ts +++ b/packages/backend/src/queue/processors/deliver.ts @@ -93,7 +93,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 422632059..420d74bb0 100644 --- a/packages/backend/src/queue/processors/inbox.ts +++ b/packages/backend/src/queue/processors/inbox.ts @@ -67,7 +67,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 d49206f68..73d66309c 100644 --- a/packages/backend/src/queue/processors/webhook-deliver.ts +++ b/packages/backend/src/queue/processors/webhook-deliver.ts @@ -50,7 +50,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 362a543ec..7c2083a27 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 5d63f2605..d5db2a2cf 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 348e9baca..4384e20f0 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 ed513eda0..f03bf592a 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 231913223..99670ae2f 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 @@ -87,14 +87,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`); } })); @@ -102,7 +102,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 140f96d57..cf5710f82 100644 --- a/packages/backend/src/server/api/endpoints/reset-db.ts +++ b/packages/backend/src/server/api/endpoints/reset-db.ts @@ -22,7 +22,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 ee1245132..4bf101bb4 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();