diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2019-02-22 13:38:12 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2019-02-22 13:38:12 +0900 |
| commit | 4920983f23cde81a4de3fa0eefbef5d061b3cf83 (patch) | |
| tree | 06278519425da84d52d1fd89b0fd368419a71b46 /src | |
| parent | Improve error handling of API (#4345) (diff) | |
| download | sharkey-4920983f23cde81a4de3fa0eefbef5d061b3cf83.tar.gz sharkey-4920983f23cde81a4de3fa0eefbef5d061b3cf83.tar.bz2 sharkey-4920983f23cde81a4de3fa0eefbef5d061b3cf83.zip | |
Refactoring
Diffstat (limited to 'src')
| -rw-r--r-- | src/server/api/call.ts | 19 |
1 files changed, 5 insertions, 14 deletions
diff --git a/src/server/api/call.ts b/src/server/api/call.ts index 2b447b30f6..46efc24bc5 100644 --- a/src/server/api/call.ts +++ b/src/server/api/call.ts @@ -4,7 +4,6 @@ import { IApp } from '../../models/app'; import endpoints from './endpoints'; import { ApiError } from './error'; import { apiLogger } from './logger'; -import { Response } from './define'; const accessDenied = { 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) { - try { - await limiter(ep, user); // Rate limit - } catch (e) { - // drop request if limit exceeded + // Rate limit + await limiter(ep, user).catch(e => { throw new ApiError({ message: 'Rate limit exceeded. Please try again later.', code: 'RATE_LIMIT_EXCEEDED', id: 'd5826d14-3982-4d2e-8011-b9e9f02499ef', }); - } + }); } - let res: Response; - // API invoking - try { - res = await ep.exec(data, user, app, file); - } catch (e) { + return await ep.exec(data, user, app, file).catch(e => { if (e instanceof ApiError) { throw e; } else { @@ -88,7 +81,5 @@ export default async (endpoint: string, user: IUser, app: IApp, data: any, file? } }); } - } - - return res; + }); }; |