From: Jérôme Benoit Date: Fri, 26 Aug 2022 22:38:32 +0000 (+0200) Subject: UI Server: factor out reponse status to http status code helper X-Git-Tag: v1.1.68~25 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=771633ffad91ad096e5b837f3b102b3251e39633;p=e-mobility-charging-stations-simulator.git UI Server: factor out reponse status to http status code helper Signed-off-by: Jérôme Benoit --- 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; + } + } }