Improve error handling of API (#4345)

* wip

* wip

* wip

* Update attached_notes.ts

* wip

* Refactor

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Update call.ts

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* ✌️

* Fix
This commit is contained in:
syuilo
2019-02-22 11:46:58 +09:00
committed by GitHub
parent fc52e95ad0
commit 2756f553c6
181 changed files with 2010 additions and 1322 deletions

View File

@@ -6,6 +6,7 @@ import User from '../../../../../models/user';
import { publishMainStream, publishReversiStream } from '../../../../../services/stream';
import { eighteight } from '../../../../../games/reversi/maps';
import define from '../../../define';
import { ApiError } from '../../../error';
export const meta = {
requireCredential: true,
@@ -19,13 +20,27 @@ export const meta = {
'en-US': 'Target user ID'
}
},
},
errors: {
noSuchUser: {
message: 'No such user.',
code: 'NO_SUCH_USER',
id: '0b4f0559-b484-4e31-9581-3f73cee89b28'
},
isYourself: {
message: 'Target user is yourself.',
code: 'TARGET_IS_YOURSELF',
id: '96fd7bd6-d2bc-426c-a865-d055dcd2828e'
},
}
};
export default define(meta, (ps, user) => new Promise(async (res, rej) => {
export default define(meta, async (ps, user) => {
// Myself
if (ps.userId.equals(user._id)) {
return rej('invalid userId param');
throw new ApiError(meta.errors.isYourself);
}
// Find session
@@ -57,9 +72,6 @@ export default define(meta, (ps, user) => new Promise(async (res, rej) => {
}
});
// Reponse
res(await packGame(game, user));
publishReversiStream(exist.parentId, 'matched', await packGame(game, exist.parentId));
const other = await Matching.count({
@@ -69,6 +81,8 @@ export default define(meta, (ps, user) => new Promise(async (res, rej) => {
if (other == 0) {
publishMainStream(user._id, 'reversiNoInvites');
}
return await packGame(game, user);
} else {
// Fetch child
const child = await User.findOne({
@@ -80,7 +94,7 @@ export default define(meta, (ps, user) => new Promise(async (res, rej) => {
});
if (child === null) {
return rej('user not found');
throw new ApiError(meta.errors.noSuchUser);
}
// 以前のセッションはすべて削除しておく
@@ -95,14 +109,10 @@ export default define(meta, (ps, user) => new Promise(async (res, rej) => {
childId: child._id
});
// Reponse
res();
const packed = await packMatching(matching, child);
// 招待
publishReversiStream(child._id, 'invited', packed);
publishMainStream(child._id, 'reversiInvited', packed);
return;
}
}));
});