From ded8d853fe2f177bf9f902d160fd041326ab5fff Mon Sep 17 00:00:00 2001
From: Robin Leber <rleber98@gmail.com>
Date: Thu, 13 Jun 2024 11:53:46 +0200
Subject: [PATCH] Scale out error handling

---
 src/functions/errors.js          | 10 ++++++++++
 src/functions/helperFunctions.js |  6 ++++++
 src/routes/deployRouter.js       |  6 +++++-
 3 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 src/functions/errors.js

diff --git a/src/functions/errors.js b/src/functions/errors.js
new file mode 100644
index 0000000..d6b16ad
--- /dev/null
+++ b/src/functions/errors.js
@@ -0,0 +1,10 @@
+class CustomError extends Error {
+  constructor(message, statusCode) {
+    super(message);
+    this.name = "CustomError";
+    this.statusCode = statusCode;
+    Error.captureStackTrace(this, this.constructor);
+  }
+}
+
+module.exports = CustomError;
diff --git a/src/functions/helperFunctions.js b/src/functions/helperFunctions.js
index 4563984..c5ddd71 100644
--- a/src/functions/helperFunctions.js
+++ b/src/functions/helperFunctions.js
@@ -10,6 +10,7 @@ const {
   killDockerContainer,
 } = require("../docker/dockerManager");
 const { v4: uuidv4 } = require("uuid");
+const CustomError = require("./errors.js");
 
 const fillDockerJS = async (code) => {
   const filePath = path.resolve(__dirname, "../../docker.js");
@@ -204,6 +205,11 @@ const scaleOut = async (id) => {
     );
     const customCodes = await getCustomCodes();
     const codeToScale = customCodes.find((code) => code.id === id);
+    if (!codeToScale) {
+      throw new CustomError("Need to code first!", 500);
+    } else if (!codeToScale.isDeployed) {
+      throw new CustomError("Need to deploy first!", 500);
+    }
     const scaledCode = JSON.parse(JSON.stringify(codeToScale));
     scaledCode.isScaled = id;
     scaledCode.id = uuidv4();
diff --git a/src/routes/deployRouter.js b/src/routes/deployRouter.js
index c0d86da..1d4f752 100644
--- a/src/routes/deployRouter.js
+++ b/src/routes/deployRouter.js
@@ -57,7 +57,11 @@ deployRouter.post("/scaleOut", async (req, res) => {
     res.status(200).json({ message: "Scale Out erfolgreich" });
   } catch (e) {
     console.error(e);
-    res.status(500).json({ message: "Scale Out fehlgeschlagen" });
+    if (e.name === "CustomError") {
+      res.status(500).json({ message: e.message });
+    } else {
+      res.status(500).json({ message: "Scale Out fehlgeschlagen" });
+    }
   }
 });
 
-- 
GitLab