From fc9ba58fb9aaecbb92869453a97805918a9af343 Mon Sep 17 00:00:00 2001 From: G Date: Fri, 6 Sep 2024 10:21:58 +0000 Subject: [PATCH] use of switch case for better flow --- src/utils/requests/orangePayment.ts | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/utils/requests/orangePayment.ts b/src/utils/requests/orangePayment.ts index 469672b..d03383b 100644 --- a/src/utils/requests/orangePayment.ts +++ b/src/utils/requests/orangePayment.ts @@ -74,20 +74,31 @@ export const getTransactionStatus = async (orderId: string) => { }); log.http("getTransactionStatus |", JSON.stringify(response, null, 2)); - if (response.status === "INITIATED") { - log.warn("Payment is still in progress, throwing error for mutation to catch"); - const error = new Error("Payment is still in progress"); - error.name = "ORANGE_PAYMENT_IN_PROGRESS"; - throw error; - } - if (response.status === "FAILED") { - log.warn("Payment failed, throwing error for mutation to catch"); - const error = new Error("Payment failed"); - error.name = "ORANGE_PAYMENT_FAILED"; - throw error; + switch (response.status) { + case "SUCCESS": { + log.http("getTransactionStatus |", JSON.stringify(response, null, 2)); + return response; + } + case "INITIATED": { + log.warn("Payment is still in progress, throwing error for mutation to catch"); + const error = new Error("Payment is still in progress"); + error.name = "ORANGE_PAYMENT_IN_PROGRESS"; + throw error; + } + case "FAILED": { + log.warn("Payment failed, throwing error for mutation to catch"); + const error = new Error("Payment failed"); + error.name = "ORANGE_PAYMENT_FAILED"; + throw error; + } + + default: { + log.warn("An unknown error occured, throwing error for mutation to catch"); + const error = new Error("Payment failed"); + error.name = "UNKNOWN_PAYMENT_ERROR"; + throw error; + } } - log.http("getTransactionStatus |", JSON.stringify(response, null, 2)); - return response; } catch (error) { log.error("getTransactionStatus |", error); throw error; -- libgit2 0.27.1