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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
181 changed files with 2010 additions and 1322 deletions

View file

@ -15,6 +15,7 @@ import Instance from '../../models/instance';
import instanceChart from '../../services/chart/instance';
import Logger from '../../misc/logger';
import FollowRequest from '../../models/follow-request';
import { IdentifiableError } from '../../misc/identifiable-error';
const logger = new Logger('following/create');
@ -142,8 +143,8 @@ export default async function(follower: IUser, followee: IUser, requestId?: stri
});
} else {
// それ以外は単純に例外
if (blocking != null) throw new Error('blocking');
if (blocked != null) throw new Error('blocked');
if (blocking != null) throw new IdentifiableError('710e8fb0-b8c3-4922-be49-d5d93d8e6a6e', 'blocking');
if (blocked != null) throw new IdentifiableError('3338392a-f764-498d-8855-db939dcf8c48', 'blocked');
}
// フォロー対象が鍵アカウントである or

View file

@ -5,6 +5,7 @@ import renderFollow from '../../../remote/activitypub/renderer/follow';
import renderUndo from '../../../remote/activitypub/renderer/undo';
import { deliver } from '../../../queue';
import { publishMainStream } from '../../stream';
import { IdentifiableError } from '../../../misc/identifiable-error';
export default async function(followee: IUser, follower: IUser) {
if (isRemoteUser(followee)) {
@ -18,7 +19,7 @@ export default async function(followee: IUser, follower: IUser) {
});
if (request == null) {
throw 'request not found';
throw new IdentifiableError('17447091-ce07-46dd-b331-c1fd4f15b1e7', 'request not found');
}
await FollowRequest.remove({

View file

@ -7,6 +7,7 @@ import renderAdd from '../../remote/activitypub/renderer/add';
import renderRemove from '../../remote/activitypub/renderer/remove';
import { renderActivity } from '../../remote/activitypub/renderer';
import { deliver } from '../../queue';
import { IdentifiableError } from '../../misc/identifiable-error';
/**
* 稿
@ -21,7 +22,7 @@ export async function addPinned(user: IUser, noteId: mongo.ObjectID) {
});
if (note === null) {
throw new Error('note not found');
throw new IdentifiableError('70c4e51f-5bea-449c-a030-53bee3cce202', 'No such note.');
}
let pinnedNoteIds = user.pinnedNoteIds || [];
@ -35,11 +36,11 @@ export async function addPinned(user: IUser, noteId: mongo.ObjectID) {
//#endregion
if (pinnedNoteIds.length >= 5) {
throw new Error('cannot pin more notes');
throw new IdentifiableError('15a018eb-58e5-4da1-93be-330fcc5e4e1a', 'You can not pin notes any more.');
}
if (pinnedNoteIds.some(id => id.equals(note._id))) {
throw new Error('already exists');
throw new IdentifiableError('23f0cf4e-59a3-4276-a91d-61a5891c1514', 'That note has already been pinned.');
}
pinnedNoteIds.unshift(note._id);
@ -69,7 +70,7 @@ export async function removePinned(user: IUser, noteId: mongo.ObjectID) {
});
if (note === null) {
throw new Error('note not found');
throw new IdentifiableError('b302d4cf-c050-400a-bbb3-be208681f40c', 'No such note.');
}
const pinnedNoteIds = (user.pinnedNoteIds || []).filter(id => !id.equals(note._id));

View file

@ -27,7 +27,6 @@ export default (user: IUser, note: INote, choice: number) => new Promise(async (
choice: choice
});
// Send response
res();
const inc: any = {};

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;
};