diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2019-04-14 04:17:24 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2019-04-14 04:17:24 +0900 |
| commit | e3b3f8fac14fb4c4d150fb31c11ead1a193a36e0 (patch) | |
| tree | 021d39efbb8ec59d098e06603bf86dae670773e0 /src/remote | |
| parent | Update CHANGELOG.md (diff) | |
| download | sharkey-e3b3f8fac14fb4c4d150fb31c11ead1a193a36e0.tar.gz sharkey-e3b3f8fac14fb4c4d150fb31c11ead1a193a36e0.tar.bz2 sharkey-e3b3f8fac14fb4c4d150fb31c11ead1a193a36e0.zip | |
Better error handling
Diffstat (limited to 'src/remote')
| -rw-r--r-- | src/remote/activitypub/kernel/accept/follow.ts | 2 | ||||
| -rw-r--r-- | src/remote/activitypub/kernel/block/index.ts | 2 | ||||
| -rw-r--r-- | src/remote/activitypub/kernel/follow.ts | 2 | ||||
| -rw-r--r-- | src/remote/activitypub/kernel/like.ts | 2 | ||||
| -rw-r--r-- | src/remote/activitypub/kernel/reject/follow.ts | 2 | ||||
| -rw-r--r-- | src/remote/activitypub/kernel/undo/block.ts | 2 | ||||
| -rw-r--r-- | src/remote/activitypub/kernel/undo/follow.ts | 2 | ||||
| -rw-r--r-- | src/remote/activitypub/kernel/undo/like.ts | 4 | ||||
| -rw-r--r-- | src/remote/activitypub/models/note.ts | 12 | ||||
| -rw-r--r-- | src/remote/activitypub/models/person.ts | 8 | ||||
| -rw-r--r-- | src/remote/activitypub/models/question.ts | 10 | ||||
| -rw-r--r-- | src/remote/resolve-user.ts | 6 |
12 files changed, 27 insertions, 27 deletions
diff --git a/src/remote/activitypub/kernel/accept/follow.ts b/src/remote/activitypub/kernel/accept/follow.ts index f3e517ad9f..377b8dac42 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<void> => { 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 19e33eb7dd..5c247326cb 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<void> => { 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 d37404502f..c255067bfd 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<void> => { 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 d4fa7bf387..a08b453a89 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 91689339ab..d8b5a4b9b9 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<void> => { 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 9c277ed7d2..8ef70a9bef 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<void> => { 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 ce84d0c791..d75f055640 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<void> => { 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 75879d697a..2678828a9a 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<void> => { 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 c11a77b0fe..8842342342 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<Note | null> { 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<Note | null> { 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 9465cf0cd0..c1c07c7bbf 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<User | null> { - 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<Use * Personを作成します。 */ export async function createPerson(uri: string, resolver?: Resolver): Promise<User> { - 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<Us * @param hint Hint of Person object (この値が正当なPersonの場合、Remote resolveをせずに更新に利用します) */ export async function updatePerson(uri: string, resolver?: Resolver | null, hint?: object): Promise<void> { - 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<User> { - 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 708cdc2a66..5c126c3a56 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 9b518f5e81..a4bfca8422 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; } |