From e3b3f8fac14fb4c4d150fb31c11ead1a193a36e0 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 14 Apr 2019 04:17:24 +0900 Subject: [PATCH] Better error handling --- src/misc/gen-id.ts | 2 +- src/prelude/ensure.ts | 2 +- src/remote/activitypub/kernel/accept/follow.ts | 2 +- src/remote/activitypub/kernel/block/index.ts | 2 +- src/remote/activitypub/kernel/follow.ts | 2 +- src/remote/activitypub/kernel/like.ts | 2 +- src/remote/activitypub/kernel/reject/follow.ts | 2 +- src/remote/activitypub/kernel/undo/block.ts | 2 +- src/remote/activitypub/kernel/undo/follow.ts | 2 +- src/remote/activitypub/kernel/undo/like.ts | 4 ++-- src/remote/activitypub/models/note.ts | 12 ++++++------ src/remote/activitypub/models/person.ts | 8 ++++---- src/remote/activitypub/models/question.ts | 10 +++++----- src/remote/resolve-user.ts | 6 +++--- src/server/api/authenticate.ts | 4 ++-- src/server/api/common/getters.ts | 4 ++-- src/server/api/stream/channels/games/reversi-game.ts | 12 ++++++------ src/services/drive/add-file.ts | 4 ++-- src/services/i/pin.ts | 2 +- src/services/i/update.ts | 2 +- src/services/note/create.ts | 6 +++--- src/services/note/polls/update.ts | 4 ++-- src/services/note/polls/vote.ts | 2 +- src/tools/add-emoji.ts | 4 ++-- src/tools/show-signin-history.ts | 2 +- 25 files changed, 52 insertions(+), 52 deletions(-) diff --git a/src/misc/gen-id.ts b/src/misc/gen-id.ts index 5ee65e817..99cb70b3f 100644 --- a/src/misc/gen-id.ts +++ b/src/misc/gen-id.ts @@ -14,6 +14,6 @@ export function genId(date?: Date): string { case 'meid': return genMeid(date); case 'ulid': return ulid(date.getTime()); case 'objectid': return genObjectId(date); - default: throw 'unknown id generation method'; + default: throw new Error('unknown id generation method'); } } diff --git a/src/prelude/ensure.ts b/src/prelude/ensure.ts index c9264adc1..1af281c05 100644 --- a/src/prelude/ensure.ts +++ b/src/prelude/ensure.ts @@ -3,7 +3,7 @@ */ export function ensure(x: T): NonNullable { if (x == null) { - throw 'ぬるぽ'; + throw new Error('ぬるぽ'); } else { return x!; } diff --git a/src/remote/activitypub/kernel/accept/follow.ts b/src/remote/activitypub/kernel/accept/follow.ts index f3e517ad9..377b8dac4 100644 --- a/src/remote/activitypub/kernel/accept/follow.ts +++ b/src/remote/activitypub/kernel/accept/follow.ts @@ -6,7 +6,7 @@ import { Users } from '../../../../models'; export default async (actor: IRemoteUser, activity: IFollow): Promise => { const id = typeof activity.actor == 'string' ? activity.actor : activity.actor.id; - if (id == null) throw 'missing id'; + if (id == null) throw new Error('missing id'); if (!id.startsWith(config.url + '/')) { return; diff --git a/src/remote/activitypub/kernel/block/index.ts b/src/remote/activitypub/kernel/block/index.ts index 19e33eb7d..5c247326c 100644 --- a/src/remote/activitypub/kernel/block/index.ts +++ b/src/remote/activitypub/kernel/block/index.ts @@ -9,7 +9,7 @@ const logger = apLogger; export default async (actor: IRemoteUser, activity: IBlock): Promise => { const id = typeof activity.object == 'string' ? activity.object : activity.object.id; - if (id == null) throw 'missing id'; + if (id == null) throw new Error('missing id'); const uri = activity.id || activity; diff --git a/src/remote/activitypub/kernel/follow.ts b/src/remote/activitypub/kernel/follow.ts index d37404502..c255067bf 100644 --- a/src/remote/activitypub/kernel/follow.ts +++ b/src/remote/activitypub/kernel/follow.ts @@ -6,7 +6,7 @@ import { Users } from '../../../models'; export default async (actor: IRemoteUser, activity: IFollow): Promise => { const id = typeof activity.object == 'string' ? activity.object : activity.object.id; - if (id == null) throw 'missing id'; + if (id == null) throw new Error('missing id'); if (!id.startsWith(config.url + '/')) { return; diff --git a/src/remote/activitypub/kernel/like.ts b/src/remote/activitypub/kernel/like.ts index d4fa7bf38..a08b453a8 100644 --- a/src/remote/activitypub/kernel/like.ts +++ b/src/remote/activitypub/kernel/like.ts @@ -5,7 +5,7 @@ import { Notes } from '../../../models'; export default async (actor: IRemoteUser, activity: ILike) => { const id = typeof activity.object == 'string' ? activity.object : activity.object.id; - if (id == null) throw 'missing id'; + if (id == null) throw new Error('missing id'); // Transform: // https://misskey.ex/notes/xxxx to diff --git a/src/remote/activitypub/kernel/reject/follow.ts b/src/remote/activitypub/kernel/reject/follow.ts index 91689339a..d8b5a4b9b 100644 --- a/src/remote/activitypub/kernel/reject/follow.ts +++ b/src/remote/activitypub/kernel/reject/follow.ts @@ -6,7 +6,7 @@ import { Users } from '../../../../models'; export default async (actor: IRemoteUser, activity: IFollow): Promise => { const id = typeof activity.actor == 'string' ? activity.actor : activity.actor.id; - if (id == null) throw 'missing id'; + if (id == null) throw new Error('missing id'); if (!id.startsWith(config.url + '/')) { return; diff --git a/src/remote/activitypub/kernel/undo/block.ts b/src/remote/activitypub/kernel/undo/block.ts index 9c277ed7d..8ef70a9be 100644 --- a/src/remote/activitypub/kernel/undo/block.ts +++ b/src/remote/activitypub/kernel/undo/block.ts @@ -9,7 +9,7 @@ const logger = apLogger; export default async (actor: IRemoteUser, activity: IBlock): Promise => { const id = typeof activity.object == 'string' ? activity.object : activity.object.id; - if (id == null) throw 'missing id'; + if (id == null) throw new Error('missing id'); const uri = activity.id || activity; diff --git a/src/remote/activitypub/kernel/undo/follow.ts b/src/remote/activitypub/kernel/undo/follow.ts index ce84d0c79..d75f05564 100644 --- a/src/remote/activitypub/kernel/undo/follow.ts +++ b/src/remote/activitypub/kernel/undo/follow.ts @@ -7,7 +7,7 @@ import { Users, FollowRequests, Followings } from '../../../../models'; export default async (actor: IRemoteUser, activity: IFollow): Promise => { const id = typeof activity.object == 'string' ? activity.object : activity.object.id; - if (id == null) throw 'missing id'; + if (id == null) throw new Error('missing id'); if (!id.startsWith(config.url + '/')) { return; diff --git a/src/remote/activitypub/kernel/undo/like.ts b/src/remote/activitypub/kernel/undo/like.ts index 75879d697..2678828a9 100644 --- a/src/remote/activitypub/kernel/undo/like.ts +++ b/src/remote/activitypub/kernel/undo/like.ts @@ -8,13 +8,13 @@ import { Notes } from '../../../../models'; */ export default async (actor: IRemoteUser, activity: ILike): Promise => { const id = typeof activity.object == 'string' ? activity.object : activity.object.id; - if (id == null) throw 'missing id'; + if (id == null) throw new Error('missing id'); const noteId = id.split('/').pop(); const note = await Notes.findOne(noteId); if (note == null) { - throw 'note not found'; + throw new Error('note not found'); } await deleteReaction(actor, note); diff --git a/src/remote/activitypub/models/note.ts b/src/remote/activitypub/models/note.ts index c11a77b0f..884234234 100644 --- a/src/remote/activitypub/models/note.ts +++ b/src/remote/activitypub/models/note.ts @@ -32,7 +32,7 @@ const logger = apLogger; */ export async function fetchNote(value: string | IObject, resolver?: Resolver): Promise { const uri = typeof value == 'string' ? value : value.id; - if (uri == null) throw 'missing uri'; + if (uri == null) throw new Error('missing uri'); // URIがこのサーバーを指しているならデータベースからフェッチ if (uri.startsWith(config.url + '/')) { @@ -67,7 +67,7 @@ export async function createNote(value: any, resolver?: Resolver, silent = false value: value, object: object }); - throw 'invalid note'; + throw new Error('invalid note'); } const note: INote = object; @@ -81,7 +81,7 @@ export async function createNote(value: any, resolver?: Resolver, silent = false // 投稿者が凍結されていたらスキップ if (actor.isSuspended) { - throw 'actor has been suspended'; + throw new Error('actor has been suspended'); } //#region Visibility @@ -124,7 +124,7 @@ export async function createNote(value: any, resolver?: Resolver, silent = false ? await resolveNote(note.inReplyTo, resolver).then(x => { if (x == null) { logger.warn(`Specified inReplyTo, but nout found`); - throw 'inReplyTo not found'; + throw new Error('inReplyTo not found'); } else { return x; } @@ -230,7 +230,7 @@ export async function createNote(value: any, resolver?: Resolver, silent = false */ export async function resolveNote(value: string | IObject, resolver?: Resolver): Promise { const uri = typeof value == 'string' ? value : value.id; - if (uri == null) throw 'missing uri'; + if (uri == null) throw new Error('missing uri'); // ブロックしてたら中断 // TODO: いちいちデータベースにアクセスするのはコスト高そうなのでどっかにキャッシュしておく @@ -252,7 +252,7 @@ export async function resolveNote(value: string | IObject, resolver?: Resolver): if (e.name === 'duplicated') { return fetchNote(uri).then(note => { if (note == null) { - throw 'something happened'; + throw new Error('something happened'); } else { return note; } diff --git a/src/remote/activitypub/models/person.ts b/src/remote/activitypub/models/person.ts index 9465cf0cd..c1c07c7bb 100644 --- a/src/remote/activitypub/models/person.ts +++ b/src/remote/activitypub/models/person.ts @@ -88,7 +88,7 @@ function validatePerson(x: any, uri: string) { * Misskeyに対象のPersonが登録されていればそれを返します。 */ export async function fetchPerson(uri: string, resolver?: Resolver): Promise { - if (typeof uri !== 'string') throw 'uri is not string'; + if (typeof uri !== 'string') throw new Error('uri is not string'); // URIがこのサーバーを指しているならデータベースからフェッチ if (uri.startsWith(config.url + '/')) { @@ -111,7 +111,7 @@ export async function fetchPerson(uri: string, resolver?: Resolver): Promise { - if (typeof uri !== 'string') throw 'uri is not string'; + if (typeof uri !== 'string') throw new Error('uri is not string'); if (resolver == null) resolver = new Resolver(); @@ -256,7 +256,7 @@ export async function createPerson(uri: string, resolver?: Resolver): Promise { - if (typeof uri !== 'string') throw 'uri is not string'; + if (typeof uri !== 'string') throw new Error('uri is not string'); // URIがこのサーバーを指しているならスキップ if (uri.startsWith(config.url + '/')) { @@ -380,7 +380,7 @@ export async function updatePerson(uri: string, resolver?: Resolver | null, hint * リモートサーバーからフェッチしてMisskeyに登録しそれを返します。 */ export async function resolvePerson(uri: string, resolver?: Resolver): Promise { - if (typeof uri !== 'string') throw 'uri is not string'; + if (typeof uri !== 'string') throw new Error('uri is not string'); //#region このサーバーに既に登録されていたらそれを返す const exist = await fetchPerson(uri); diff --git a/src/remote/activitypub/models/question.ts b/src/remote/activitypub/models/question.ts index 708cdc2a6..5c126c3a5 100644 --- a/src/remote/activitypub/models/question.ts +++ b/src/remote/activitypub/models/question.ts @@ -11,7 +11,7 @@ export async function extractPollFromQuestion(source: string | IQuestion): Promi const expiresAt = question.endTime ? new Date(question.endTime) : null; if (multiple && !question.anyOf) { - throw 'invalid question'; + throw new Error('invalid question'); } const choices = question[multiple ? 'anyOf' : 'oneOf']! @@ -37,14 +37,14 @@ export async function updateQuestion(value: any) { const uri = typeof value == 'string' ? value : value.id; // URIがこのサーバーを指しているならスキップ - if (uri.startsWith(config.url + '/')) throw 'uri points local'; + if (uri.startsWith(config.url + '/')) throw new Error('uri points local'); //#region このサーバーに既に登録されているか const note = await Notes.findOne({ uri }); - if (note == null) throw 'Question is not registed'; + if (note == null) throw new Error('Question is not registed'); const poll = await Polls.findOne({ noteId: note.id }); - if (poll == null) throw 'Question is not registed'; + if (poll == null) throw new Error('Question is not registed'); //#endregion // resolve new Question object @@ -52,7 +52,7 @@ export async function updateQuestion(value: any) { const question = await resolver.resolve(value) as IQuestion; apLogger.debug(`fetched question: ${JSON.stringify(question, null, 2)}`); - if (question.type !== 'Question') throw 'object is not a Question'; + if (question.type !== 'Question') throw new Error('object is not a Question'); const apChoices = question.oneOf || question.anyOf; diff --git a/src/remote/resolve-user.ts b/src/remote/resolve-user.ts index 9b518f5e8..a4bfca842 100644 --- a/src/remote/resolve-user.ts +++ b/src/remote/resolve-user.ts @@ -17,7 +17,7 @@ export async function resolveUser(username: string, host: string | null, option? logger.info(`return local user: ${usernameLower}`); return await Users.findOne({ usernameLower, host: null }).then(u => { if (u == null) { - throw 'user not found'; + throw new Error('user not found'); } else { return u; } @@ -30,7 +30,7 @@ export async function resolveUser(username: string, host: string | null, option? logger.info(`return local user: ${usernameLower}`); return await Users.findOne({ usernameLower, host: null }).then(u => { if (u == null) { - throw 'user not found'; + throw new Error('user not found'); } else { return u; } @@ -78,7 +78,7 @@ export async function resolveUser(username: string, host: string | null, option? logger.info(`return resynced remote user: ${acctLower}`); return await Users.findOne({ uri: self.href }).then(u => { if (u == null) { - throw 'user not found'; + throw new Error('user not found'); } else { return u; } diff --git a/src/server/api/authenticate.ts b/src/server/api/authenticate.ts index 5c2b7e45c..ecf4a82c4 100644 --- a/src/server/api/authenticate.ts +++ b/src/server/api/authenticate.ts @@ -14,7 +14,7 @@ export default async (token: string): Promise<[User | null | undefined, App | nu .findOne({ token }); if (user == null) { - throw 'user not found'; + throw new Error('user not found'); } return [user, null]; @@ -24,7 +24,7 @@ export default async (token: string): Promise<[User | null | undefined, App | nu }); if (accessToken == null) { - throw 'invalid signature'; + throw new Error('invalid signature'); } const app = await Apps diff --git a/src/server/api/common/getters.ts b/src/server/api/common/getters.ts index b720840eb..04716d19c 100644 --- a/src/server/api/common/getters.ts +++ b/src/server/api/common/getters.ts @@ -36,7 +36,7 @@ export async function getRemoteUser(userId: User['id']) { const user = await getUser(userId); if (!Users.isRemoteUser(user)) { - throw 'user is not a remote user'; + throw new Error('user is not a remote user'); } return user; @@ -49,7 +49,7 @@ export async function getLocalUser(userId: User['id']) { const user = await getUser(userId); if (!Users.isLocalUser(user)) { - throw 'user is not a local user'; + throw new Error('user is not a local user'); } return user; diff --git a/src/server/api/stream/channels/games/reversi-game.ts b/src/server/api/stream/channels/games/reversi-game.ts index d708eae9f..7c13666c5 100644 --- a/src/server/api/stream/channels/games/reversi-game.ts +++ b/src/server/api/stream/channels/games/reversi-game.ts @@ -43,7 +43,7 @@ export default class extends Channel { if (this.user == null) return; const game = await ReversiGames.findOne(this.gameId!); - if (game == null) throw 'game not found'; + if (game == null) throw new Error('game not found'); if (game.isStarted) return; if ((game.user1Id !== this.user.id) && (game.user2Id !== this.user.id)) return; @@ -67,7 +67,7 @@ export default class extends Channel { if (this.user == null) return; const game = await ReversiGames.findOne(this.gameId!); - if (game == null) throw 'game not found'; + if (game == null) throw new Error('game not found'); if (game.isStarted) return; if ((game.user1Id !== this.user.id) && (game.user2Id !== this.user.id)) return; @@ -91,7 +91,7 @@ export default class extends Channel { if (this.user == null) return; const game = await ReversiGames.findOne(this.gameId!); - if (game == null) throw 'game not found'; + if (game == null) throw new Error('game not found'); if (game.isStarted) return; if ((game.user1Id !== this.user.id) && (game.user2Id !== this.user.id)) return; @@ -135,7 +135,7 @@ export default class extends Channel { if (this.user == null) return; const game = await ReversiGames.findOne(this.gameId!); - if (game == null) throw 'game not found'; + if (game == null) throw new Error('game not found'); if (game.isStarted) return; @@ -237,7 +237,7 @@ export default class extends Channel { if (this.user == null) return; const game = await ReversiGames.findOne(this.gameId!); - if (game == null) throw 'game not found'; + if (game == null) throw new Error('game not found'); if (!game.isStarted) return; if (game.isEnded) return; @@ -304,7 +304,7 @@ export default class extends Channel { @autobind private async check(crc32: string) { const game = await ReversiGames.findOne(this.gameId!); - if (game == null) throw 'game not found'; + if (game == null) throw new Error('game not found'); if (!game.isStarted) return; diff --git a/src/services/drive/add-file.ts b/src/services/drive/add-file.ts index 94b97fed6..9eccfb125 100644 --- a/src/services/drive/add-file.ts +++ b/src/services/drive/add-file.ts @@ -297,7 +297,7 @@ export default async function( // If usage limit exceeded if (usage + size > driveCapacity) { if (Users.isLocalUser(user)) { - throw 'no-free-space'; + throw new Error('no-free-space'); } else { // (アバターまたはバナーを含まず)最も古いファイルを削除する deleteOldFile(user as IRemoteUser); @@ -316,7 +316,7 @@ export default async function( userId: user.id }); - if (driveFolder == null) throw 'folder-not-found'; + if (driveFolder == null) throw new Error('folder-not-found'); return driveFolder; }; diff --git a/src/services/i/pin.ts b/src/services/i/pin.ts index f5c957c74..a6d2dfcdb 100644 --- a/src/services/i/pin.ts +++ b/src/services/i/pin.ts @@ -78,7 +78,7 @@ export async function removePinned(user: User, noteId: Note['id']) { export async function deliverPinnedChange(userId: User['id'], noteId: Note['id'], isAddition: boolean) { const user = await Users.findOne(userId); - if (user == null) throw 'user not found'; + if (user == null) throw new Error('user not found'); if (!Users.isLocalUser(user)) return; diff --git a/src/services/i/update.ts b/src/services/i/update.ts index 2bb5c7a10..ddb6704a0 100644 --- a/src/services/i/update.ts +++ b/src/services/i/update.ts @@ -7,7 +7,7 @@ import { renderPerson } from '../../remote/activitypub/renderer/person'; export async function publishToFollowers(userId: User['id']) { const user = await Users.findOne(userId); - if (user == null) throw 'user not found'; + if (user == null) throw new Error('user not found'); const followers = await Followings.find({ followeeId: user.id diff --git a/src/services/note/create.ts b/src/services/note/create.ts index 9c5989696..8c85a5c27 100644 --- a/src/services/note/create.ts +++ b/src/services/note/create.ts @@ -175,7 +175,7 @@ export default async (user: User, data: Option, silent = false) => new Promise x.id === u.id)) { @@ -214,7 +214,7 @@ export default async (user: User, data: Option, silent = false) => new Promise { console.log('success'); diff --git a/src/tools/show-signin-history.ts b/src/tools/show-signin-history.ts index 9e6e849f5..fd7cd39e3 100644 --- a/src/tools/show-signin-history.ts +++ b/src/tools/show-signin-history.ts @@ -15,7 +15,7 @@ async function main(username: string, headers?: string[]) { usernameLower: username.toLowerCase(), }); - if (user == null) throw 'User not found'; + if (user == null) throw new Error('User not found'); const history = await Signins.find({ userId: user.id