diff options
| author | Kagami Sascha Rosylight <saschanaz@outlook.com> | 2023-03-06 07:51:17 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-06 07:51:17 +0100 |
| commit | 5651353c2756e6580a819c7d4f1f5ad41553abbe (patch) | |
| tree | 7055c10a2af3734be765508e1212aa9c23ec34ee /packages/backend/src/server/api/ApiServerService.ts | |
| parent | Merge branch 'develop' into mkusername-empty (diff) | |
| parent | [ci skip] chore(client): showNoteActionsOnlyHover変更時にリロードダ... (diff) | |
| download | misskey-5651353c2756e6580a819c7d4f1f5ad41553abbe.tar.gz misskey-5651353c2756e6580a819c7d4f1f5ad41553abbe.tar.bz2 misskey-5651353c2756e6580a819c7d4f1f5ad41553abbe.zip | |
Merge branch 'develop' into mkusername-empty
Diffstat (limited to 'packages/backend/src/server/api/ApiServerService.ts')
| -rw-r--r-- | packages/backend/src/server/api/ApiServerService.ts | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/packages/backend/src/server/api/ApiServerService.ts b/packages/backend/src/server/api/ApiServerService.ts index 2b99da01b6..115d60986c 100644 --- a/packages/backend/src/server/api/ApiServerService.ts +++ b/packages/backend/src/server/api/ApiServerService.ts @@ -73,28 +73,32 @@ export class ApiServerService { Params: { endpoint: string; }, Body: Record<string, unknown>, Querystring: Record<string, unknown>, - }>('/' + endpoint.name, (request, reply) => { + }>('/' + endpoint.name, async (request, reply) => { if (request.method === 'GET' && !endpoint.meta.allowGet) { reply.code(405); reply.send(); return; } - - this.apiCallService.handleMultipartRequest(ep, request, reply); + + // Await so that any error can automatically be translated to HTTP 500 + await this.apiCallService.handleMultipartRequest(ep, request, reply); + return reply; }); } else { fastify.all<{ Params: { endpoint: string; }, Body: Record<string, unknown>, Querystring: Record<string, unknown>, - }>('/' + endpoint.name, { bodyLimit: 1024 * 32 }, (request, reply) => { + }>('/' + endpoint.name, { bodyLimit: 1024 * 32 }, async (request, reply) => { if (request.method === 'GET' && !endpoint.meta.allowGet) { reply.code(405); reply.send(); return; } - - this.apiCallService.handleRequest(ep, request, reply); + + // Await so that any error can automatically be translated to HTTP 500 + await this.apiCallService.handleRequest(ep, request, reply); + return reply; }); } } @@ -160,6 +164,22 @@ export class ApiServerService { } }); + // Make sure any unknown path under /api returns HTTP 404 Not Found, + // because otherwise ClientServerService will return the base client HTML + // page with HTTP 200. + fastify.get('*', (request, reply) => { + reply.code(404); + // Mock ApiCallService.send's error handling + reply.send({ + error: { + message: 'Unknown API endpoint.', + code: 'UNKNOWN_API_ENDPOINT', + id: '2ca3b769-540a-4f08-9dd5-b5a825b6d0f1', + kind: 'client', + }, + }); + }); + done(); } } |