From fce7dc0f4e69053c7306289189b4fa4891f57339 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 4 Oct 2018 13:53:48 +0900 Subject: [PATCH] Improve error handling of pack function of notification --- src/models/favorite.ts | 2 +- src/models/notification.ts | 12 ++++++++++++ src/server/api/endpoints/i/notifications.ts | 4 ++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/models/favorite.ts b/src/models/favorite.ts index 2c07959be7..2c10674bcb 100644 --- a/src/models/favorite.ts +++ b/src/models/favorite.ts @@ -79,7 +79,7 @@ export const pack = ( // (データベースの不具合などで)投稿が見つからなかったら if (_favorite.note == null) { - console.warn(`favorite: note not found on database: ${_favorite.noteId}`); + console.warn(`in packaging favorite: note not found on database: ${_favorite.noteId}`); return resolve(null); } diff --git a/src/models/notification.ts b/src/models/notification.ts index 835c89cd56..57be4bef10 100644 --- a/src/models/notification.ts +++ b/src/models/notification.ts @@ -77,6 +77,12 @@ export async function deleteNotification(notification: string | mongo.ObjectID | }); } +export const packMany = async ( + notifications: any[] +) => { + return (await Promise.all(notifications.map(n => pack(n)))).filter(x => x != null); +}; + /** * Pack a notification for API response */ @@ -123,6 +129,12 @@ export const pack = (notification: any) => new Promise(async (resolve, reje case 'poll_vote': // Populate note _notification.note = await packNote(_notification.noteId, me); + + // (データベースの不具合などで)投稿が見つからなかったら + if (_notification.note == null) { + console.warn(`in packaging notification: note not found on database: ${_notification.noteId}`); + return resolve(null); + } break; default: console.error(`Unknown type: ${_notification.type}`); diff --git a/src/server/api/endpoints/i/notifications.ts b/src/server/api/endpoints/i/notifications.ts index 46242b9d9f..5cc836e362 100644 --- a/src/server/api/endpoints/i/notifications.ts +++ b/src/server/api/endpoints/i/notifications.ts @@ -1,7 +1,7 @@ import $ from 'cafy'; import ID from '../../../../misc/cafy-id'; import Notification from '../../../../models/notification'; import Mute from '../../../../models/mute'; -import { pack } from '../../../../models/notification'; +import { packMany } from '../../../../models/notification'; import { getFriendIds } from '../../common/get-friends'; import read from '../../common/read-notification'; import { ILocalUser } from '../../../../models/user'; @@ -83,7 +83,7 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = }); // Serialize - res(await Promise.all(notifications.map(notification => pack(notification)))); + res(await packMany(notifications)); // Mark all as read if (notifications.length > 0 && markAsRead) {