Refactoring

This commit is contained in:
syuilo 2019-02-22 13:38:12 +09:00
parent 2756f553c6
commit 4920983f23
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69

View file

@ -4,7 +4,6 @@ import { IApp } from '../../models/app';
import endpoints from './endpoints'; import endpoints from './endpoints';
import { ApiError } from './error'; import { ApiError } from './error';
import { apiLogger } from './logger'; import { apiLogger } from './logger';
import { Response } from './define';
const accessDenied = { const accessDenied = {
message: 'Access denied.', message: 'Access denied.',
@ -58,24 +57,18 @@ export default async (endpoint: string, user: IUser, app: IApp, data: any, file?
} }
if (ep.meta.requireCredential && ep.meta.limit) { if (ep.meta.requireCredential && ep.meta.limit) {
try { // Rate limit
await limiter(ep, user); // Rate limit await limiter(ep, user).catch(e => {
} catch (e) {
// drop request if limit exceeded
throw new ApiError({ throw new ApiError({
message: 'Rate limit exceeded. Please try again later.', message: 'Rate limit exceeded. Please try again later.',
code: 'RATE_LIMIT_EXCEEDED', code: 'RATE_LIMIT_EXCEEDED',
id: 'd5826d14-3982-4d2e-8011-b9e9f02499ef', id: 'd5826d14-3982-4d2e-8011-b9e9f02499ef',
}); });
} });
} }
let res: Response;
// API invoking // API invoking
try { return await ep.exec(data, user, app, file).catch(e => {
res = await ep.exec(data, user, app, file);
} catch (e) {
if (e instanceof ApiError) { if (e instanceof ApiError) {
throw e; throw e;
} else { } else {
@ -88,7 +81,5 @@ export default async (endpoint: string, user: IUser, app: IApp, data: any, file?
} }
}); });
} }
} });
return res;
}; };