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

@ -1,11 +1,10 @@
import $ from 'cafy';
import ID, { transform } from '../../../../misc/cafy-id';
import getHostLower from '../../common/get-host-lower';
import Note, { packMany } from '../../../../models/note';
import User from '../../../../models/user';
import define from '../../define';
import { countIf } from '../../../../prelude/array';
import Following from '../../../../models/following';
import { ApiError } from '../../error';
export const meta = {
desc: {
@ -14,7 +13,7 @@ export const meta = {
params: {
userId: {
validator: $.optional.type(ID),
validator: $.type(ID),
transform: transform,
desc: {
'ja-JP': '対象のユーザーのID',
@ -22,17 +21,6 @@ export const meta = {
}
},
username: {
validator: $.optional.str,
desc: {
'ja-JP': 'ユーザー名'
}
},
host: {
validator: $.optional.nullable.str,
},
includeReplies: {
validator: $.optional.bool,
default: true,
@ -134,32 +122,27 @@ export const meta = {
'ja-JP': 'true にすると、NSFW指定されたファイルを除外します(fileTypeが指定されている場合のみ有効)'
}
},
},
errors: {
noSuchUser: {
message: 'No such user.',
code: 'NO_SUCH_USER',
id: '27e494ba-2ac2-48e8-893b-10d4d8c2387b'
}
}
};
export default define(meta, (ps, me) => new Promise(async (res, rej) => {
if (ps.userId === undefined && ps.username === undefined) {
return rej('userId or username is required');
}
// Check if only one of sinceId, untilId, sinceDate, untilDate specified
if (countIf(x => x != null, [ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate]) > 1) {
throw 'only one of sinceId, untilId, sinceDate, untilDate can be specified';
}
const q = ps.userId != null
? { _id: ps.userId }
: { usernameLower: ps.username.toLowerCase(), host: getHostLower(ps.host) } ;
export default define(meta, async (ps, me) => {
// Lookup user
const user = await User.findOne(q, {
const user = await User.findOne({ _id: ps.userId }, {
fields: {
_id: true
}
});
if (user === null) {
return rej('user not found');
throw new ApiError(meta.errors.noSuchUser);
}
const isFollowing = me == null ? false : ((await Following.findOne({
@ -259,13 +242,10 @@ export default define(meta, (ps, me) => new Promise(async (res, rej) => {
}
//#endregion
// Issue query
const notes = await Note
.find(query, {
limit: ps.limit,
sort: sort
});
const notes = await Note.find(query, {
limit: ps.limit,
sort: sort
});
// Serialize
res(await packMany(notes, me));
}));
return await packMany(notes, me);
});