From bbf4e1c41339738ce2563c3c7ae427ba4997bf3f Mon Sep 17 00:00:00 2001
From: syuilo <syuilotan@yahoo.co.jp>
Date: Wed, 6 Mar 2019 23:23:50 +0900
Subject: [PATCH] Add fallback for broken db

---
 src/models/app.ts | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/src/models/app.ts b/src/models/app.ts
index 5c8d2cbd56..45d50bccda 100644
--- a/src/models/app.ts
+++ b/src/models/app.ts
@@ -4,6 +4,7 @@ import AccessToken from './access-token';
 import db from '../db/mongodb';
 import isObjectId from '../misc/is-objectid';
 import config from '../config';
+import { dbLogger } from '../db/logger';
 
 const App = db.get<IApp>('apps');
 App.createIndex('secret');
@@ -66,6 +67,12 @@ export const pack = (
 		}
 	}
 
+	// (データベースの欠損などで)アプリがデータベース上に見つからなかったとき
+	if (_app == null) {
+		dbLogger.warn(`[DAMAGED DB] (missing) pkg: app :: ${app}`);
+		return null;
+	}
+
 	// Rename _id to id
 	_app.id = _app._id;
 	delete _app._id;