From 1a984de8e83c615faa631d868b242eb679e4eea6 Mon Sep 17 00:00:00 2001
From: syuilo <syuilotan@yahoo.co.jp>
Date: Tue, 18 Jun 2019 13:58:59 +0900
Subject: [PATCH] Better error handling

---
 src/server/api/endpoints/i/update-email.ts | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/src/server/api/endpoints/i/update-email.ts b/src/server/api/endpoints/i/update-email.ts
index 56284499d3..ca95e612a3 100644
--- a/src/server/api/endpoints/i/update-email.ts
+++ b/src/server/api/endpoints/i/update-email.ts
@@ -8,6 +8,7 @@ import * as bcrypt from 'bcryptjs';
 import { Users, UserProfiles } from '../../../../models';
 import { ensure } from '../../../../prelude/ensure';
 import { sendEmail } from '../../../../services/send-email';
+import { ApiError } from '../../error';
 
 export const meta = {
 	requireCredential: true,
@@ -27,6 +28,14 @@ export const meta = {
 		email: {
 			validator: $.optional.nullable.str
 		},
+	},
+
+	errors: {
+		incorrectPassword: {
+			message: 'Incorrect password.',
+			code: 'INCORRECT_PASSWORD',
+			id: 'e54c1d7e-e7d6-4103-86b6-0a95069b4ad3'
+		},
 	}
 };
 
@@ -37,7 +46,7 @@ export default define(meta, async (ps, user) => {
 	const same = await bcrypt.compare(ps.password, profile.password!);
 
 	if (!same) {
-		throw new Error('incorrect password');
+		throw new ApiError(meta.errors.incorrectPassword);
 	}
 
 	await UserProfiles.update({ userId: user.id }, {