From 771633ffad91ad096e5b837f3b102b3251e39633 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 27 Aug 2022 00:38:32 +0200 Subject: [PATCH] UI Server: factor out reponse status to http status code helper MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ui-server/UIHttpServer.ts | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/charging-station/ui-server/UIHttpServer.ts b/src/charging-station/ui-server/UIHttpServer.ts index 0731b3c0..2d729bfc 100644 --- a/src/charging-station/ui-server/UIHttpServer.ts +++ b/src/charging-station/ui-server/UIHttpServer.ts @@ -48,16 +48,7 @@ export default class UIHttpServer extends AbstractUIServer { public sendResponse(response: string): void { const [uuid, payload] = JSON.parse(response) as ProtocolResponse; - let statusCode: StatusCodes; - switch (payload.status) { - case ResponseStatus.SUCCESS: - statusCode = StatusCodes.OK; - break; - case ResponseStatus.FAILURE: - default: - statusCode = StatusCodes.BAD_REQUEST; - break; - } + const statusCode = this.responseStatusToStatusCode(payload.status); if (this.responseHandlers.has(uuid)) { const { res } = this.responseHandlers.get(uuid); res.writeHead(statusCode, { 'Content-Type': 'application/json' }); @@ -137,4 +128,15 @@ export default class UIHttpServer extends AbstractUIServer { private buildResponse(id: string, responsePayload: ResponsePayload): string { return JSON.stringify([id, responsePayload]); } + + private responseStatusToStatusCode(status: ResponseStatus): StatusCodes { + switch (status) { + case ResponseStatus.SUCCESS: + return StatusCodes.OK; + case ResponseStatus.FAILURE: + return StatusCodes.BAD_REQUEST; + default: + return StatusCodes.INTERNAL_SERVER_ERROR; + } + } } -- 2.34.1