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,8 +1,9 @@
import $ from 'cafy';
import ID, { transform } from '../../../../misc/cafy-id';
import Note from '../../../../models/note';
import Reaction, { pack } from '../../../../models/note-reaction';
import define from '../../define';
import { getValiedNote } from '../../common/getters';
import { ApiError } from '../../error';
export const meta = {
desc: {
@ -41,24 +42,23 @@ export const meta = {
validator: $.optional.type(ID),
transform: transform,
},
},
errors: {
noSuchNote: {
message: 'No such note.',
code: 'NO_SUCH_NOTE',
id: '263fff3d-d0e1-4af4-bea7-8408059b451a'
}
}
};
export default define(meta, (ps, user) => new Promise(async (res, rej) => {
// Check if both of sinceId and untilId is specified
if (ps.sinceId && ps.untilId) {
return rej('cannot set sinceId and untilId');
}
// Lookup note
const note = await Note.findOne({
_id: ps.noteId
export default define(meta, async (ps, user) => {
const note = await getValiedNote(ps.noteId).catch(e => {
if (e.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError(meta.errors.noSuchNote);
throw e;
});
if (note === null) {
return rej('note not found');
}
const query = {
noteId: note._id
} as any;
@ -78,13 +78,11 @@ export default define(meta, (ps, user) => new Promise(async (res, rej) => {
};
}
const reactions = await Reaction
.find(query, {
limit: ps.limit,
skip: ps.offset,
sort: sort
});
const reactions = await Reaction.find(query, {
limit: ps.limit,
skip: ps.offset,
sort: sort
});
// Serialize
res(await Promise.all(reactions.map(reaction => pack(reaction, user))));
}));
return await Promise.all(reactions.map(reaction => pack(reaction, user)));
});