UI Server: factor out reponse status to http status code helper
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 26 Aug 2022 22:38:32 +0000 (00:38 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 26 Aug 2022 22:38:32 +0000 (00:38 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ui-server/UIHttpServer.ts

index 0731b3c09712f7df1841e679227fb3b30c642184..2d729bfc3be22b57ca4a0e71e26702245e5fedd2 100644 (file)
@@ -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;
+    }
+  }
 }