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

@@ -9,31 +9,28 @@ import renderLike from '../../../remote/activitypub/renderer/like';
import { deliver } from '../../../queue';
import { renderActivity } from '../../../remote/activitypub/renderer';
import perUserReactionsChart from '../../../services/chart/per-user-reactions';
import { IdentifiableError } from '../../../misc/identifiable-error';
export default async (user: IUser, note: INote, reaction: string) => new Promise(async (res, rej) => {
export default async (user: IUser, note: INote, reaction: string) => {
// Myself
if (note.userId.equals(user._id)) {
return rej('cannot react to my note');
throw new IdentifiableError('2d8e7297-1873-4c00-8404-792c68d7bef0', 'cannot react to my note');
}
// Create reaction
try {
await NoteReaction.insert({
createdAt: new Date(),
noteId: note._id,
userId: user._id,
reaction
});
} catch (e) {
await NoteReaction.insert({
createdAt: new Date(),
noteId: note._id,
userId: user._id,
reaction
}).catch(e => {
// duplicate key error
if (e.code === 11000) {
return rej('already reacted');
throw new IdentifiableError('51c42bb4-931a-456b-bff7-e5a8a70dd298', 'already reacted');
}
return rej('something happened');
}
res();
throw e;
});
// Increment reactions count
await Note.update({ _id: note._id }, {
@@ -89,4 +86,6 @@ export default async (user: IUser, note: INote, reaction: string) => new Promise
deliver(user, content, note._user.inbox);
}
//#endregion
});
return;
};

View File

@@ -6,8 +6,9 @@ import renderLike from '../../../remote/activitypub/renderer/like';
import renderUndo from '../../../remote/activitypub/renderer/undo';
import { renderActivity } from '../../../remote/activitypub/renderer';
import { deliver } from '../../../queue';
import { IdentifiableError } from '../../../misc/identifiable-error';
export default async (user: IUser, note: INote) => new Promise(async (res, rej) => {
export default async (user: IUser, note: INote) => {
// if already unreacted
const exist = await Reaction.findOne({
noteId: note._id,
@@ -16,7 +17,7 @@ export default async (user: IUser, note: INote) => new Promise(async (res, rej)
});
if (exist === null) {
return rej('never reacted');
throw new IdentifiableError('60527ec9-b4cb-4a88-a6bd-32d3ad26817d', 'not reacted');
}
// Delete reaction
@@ -24,8 +25,6 @@ export default async (user: IUser, note: INote) => new Promise(async (res, rej)
_id: exist._id
});
res();
const dec: any = {};
dec[`reactionCounts.${exist.reaction}`] = -1;
@@ -46,4 +45,6 @@ export default async (user: IUser, note: INote) => new Promise(async (res, rej)
deliver(user, content, note._user.inbox);
}
//#endregion
});
return;
};