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
|
@ -5,6 +5,7 @@ import User, { pack } from '../../../../models/user';
|
|||
import Blocking from '../../../../models/blocking';
|
||||
import deleteBlocking from '../../../../services/blocking/delete';
|
||||
import define from '../../define';
|
||||
import { ApiError } from '../../error';
|
||||
|
||||
export const meta = {
|
||||
stability: 'stable',
|
||||
|
@ -32,15 +33,35 @@ export const meta = {
|
|||
'en-US': 'Target user ID'
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
errors: {
|
||||
noSuchUser: {
|
||||
message: 'No such user.',
|
||||
code: 'NO_SUCH_USER',
|
||||
id: '8621d8bf-c358-4303-a066-5ea78610eb3f'
|
||||
},
|
||||
|
||||
blockeeIsYourself: {
|
||||
message: 'Blockee is yourself.',
|
||||
code: 'BLOCKEE_IS_YOURSELF',
|
||||
id: '06f6fac6-524b-473c-a354-e97a40ae6eac'
|
||||
},
|
||||
|
||||
notBlocking: {
|
||||
message: 'You are not blocking that user.',
|
||||
code: 'NOT_BLOCKING',
|
||||
id: '291b2efa-60c6-45c0-9f6a-045c8f9b02cd'
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
export default define(meta, (ps, user) => new Promise(async (res, rej) => {
|
||||
export default define(meta, async (ps, user) => {
|
||||
const blocker = user;
|
||||
|
||||
// Check if the blockee is yourself
|
||||
if (user._id.equals(ps.userId)) {
|
||||
return rej('blockee is yourself');
|
||||
throw new ApiError(meta.errors.blockeeIsYourself);
|
||||
}
|
||||
|
||||
// Get blockee
|
||||
|
@ -54,7 +75,7 @@ export default define(meta, (ps, user) => new Promise(async (res, rej) => {
|
|||
});
|
||||
|
||||
if (blockee === null) {
|
||||
return rej('user not found');
|
||||
throw new ApiError(meta.errors.noSuchUser);
|
||||
}
|
||||
|
||||
// Check not blocking
|
||||
|
@ -64,14 +85,13 @@ export default define(meta, (ps, user) => new Promise(async (res, rej) => {
|
|||
});
|
||||
|
||||
if (exist === null) {
|
||||
return rej('already not blocking');
|
||||
throw new ApiError(meta.errors.notBlocking);
|
||||
}
|
||||
|
||||
// Delete blocking
|
||||
await deleteBlocking(blocker, blockee);
|
||||
|
||||
// Send response
|
||||
res(await pack(blockee._id, user, {
|
||||
return await pack(blockee._id, user, {
|
||||
detail: true
|
||||
}));
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue