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:
parent
fc52e95ad0
commit
2756f553c6
181 changed files with 2010 additions and 1322 deletions
|
|
@ -4,6 +4,7 @@ import define from '../../define';
|
|||
import User from '../../../../models/user';
|
||||
import AbuseUserReport from '../../../../models/abuse-user-report';
|
||||
import { publishAdminStream } from '../../../../services/stream';
|
||||
import { ApiError } from '../../error';
|
||||
|
||||
export const meta = {
|
||||
desc: {
|
||||
|
|
@ -28,25 +29,45 @@ export const meta = {
|
|||
'ja-JP': '迷惑行為の詳細'
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
errors: {
|
||||
noSuchUser: {
|
||||
message: 'No such user.',
|
||||
code: 'NO_SUCH_USER',
|
||||
id: '1acefcb5-0959-43fd-9685-b48305736cb5'
|
||||
},
|
||||
|
||||
cannotReportYourself: {
|
||||
message: 'Cannot report yourself.',
|
||||
code: 'CANNOT_REPORT_YOURSELF',
|
||||
id: '1e13149e-b1e8-43cf-902e-c01dbfcb202f'
|
||||
},
|
||||
|
||||
cannotReportAdmin: {
|
||||
message: 'Cannot report the admin.',
|
||||
code: 'CANNOT_REPORT_THE_ADMIN',
|
||||
id: '35e166f5-05fb-4f87-a2d5-adb42676d48f'
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default define(meta, (ps, me) => new Promise(async (res, rej) => {
|
||||
export default define(meta, async (ps, me) => {
|
||||
// Lookup user
|
||||
const user = await User.findOne({
|
||||
_id: ps.userId
|
||||
});
|
||||
|
||||
if (user === null) {
|
||||
return rej('user not found');
|
||||
throw new ApiError(meta.errors.noSuchUser);
|
||||
}
|
||||
|
||||
if (user._id.equals(me._id)) {
|
||||
return rej('cannot report yourself');
|
||||
throw new ApiError(meta.errors.cannotReportYourself);
|
||||
}
|
||||
|
||||
if (user.isAdmin) {
|
||||
return rej('cannot report admin');
|
||||
throw new ApiError(meta.errors.cannotReportAdmin);
|
||||
}
|
||||
|
||||
const report = await AbuseUserReport.insert({
|
||||
|
|
@ -65,6 +86,7 @@ export default define(meta, (ps, me) => new Promise(async (res, rej) => {
|
|||
isModerator: true
|
||||
}]
|
||||
});
|
||||
|
||||
for (const moderator of moderators) {
|
||||
publishAdminStream(moderator._id, 'newAbuseUserReport', {
|
||||
id: report._id,
|
||||
|
|
@ -74,6 +96,4 @@ export default define(meta, (ps, me) => new Promise(async (res, rej) => {
|
|||
});
|
||||
}
|
||||
}, 1);
|
||||
|
||||
res();
|
||||
}));
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue