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

@@ -3,6 +3,7 @@ import ID, { transform } from '../../../../../misc/cafy-id';
import Favorite from '../../../../../models/favorite';
import Note from '../../../../../models/note';
import define from '../../../define';
import { ApiError } from '../../../error';
export const meta = {
stability: 'stable',
@@ -25,17 +26,31 @@ export const meta = {
'en-US': 'Target note ID.'
}
}
},
errors: {
noSuchNote: {
message: 'No such note.',
code: 'NO_SUCH_NOTE',
id: '80848a2c-398f-4343-baa9-df1d57696c56'
},
notFavorited: {
message: 'You have not marked that note a favorite.',
code: 'NOT_FAVORITED',
id: 'b625fc69-635e-45e9-86f4-dbefbef35af5'
},
}
};
export default define(meta, (ps, user) => new Promise(async (res, rej) => {
export default define(meta, async (ps, user) => {
// Get favoritee
const note = await Note.findOne({
_id: ps.noteId
});
if (note === null) {
return rej('note not found');
throw new ApiError(meta.errors.noSuchNote);
}
// if already favorited
@@ -45,7 +60,7 @@ export default define(meta, (ps, user) => new Promise(async (res, rej) => {
});
if (exist === null) {
return rej('already not favorited');
throw new ApiError(meta.errors.notFavorited);
}
// Delete favorite
@@ -53,6 +68,5 @@ export default define(meta, (ps, user) => new Promise(async (res, rej) => {
_id: exist._id
});
// Send response
res();
}));
return;
});