From 3f64fb057318aa77e0cbc46f29ba813c63268550 Mon Sep 17 00:00:00 2001
From: Bastien DUMONT <bdumont@grandlyon.com>
Date: Thu, 28 Sep 2023 11:50:51 +0000
Subject: [PATCH] fix: throw vendor down on auth

---
 src/index.js | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/src/index.js b/src/index.js
index 9a1d7e9..1053b32 100644
--- a/src/index.js
+++ b/src/index.js
@@ -202,6 +202,7 @@ async function buildAggregatedData(data, doctype) {
 }
 
 async function authenticate(login, password, baseUrl, apiAuthKey) {
+  log('info', 'Authenticating ...')
   const authRequest = {
     method: 'post',
     url: baseUrl + '/connect.aspx',
@@ -219,13 +220,12 @@ async function authenticate(login, password, baseUrl, apiAuthKey) {
     const resp = await axios(authRequest)
     if (resp.data.codeRetour === 100) {
       return resp.data
-    } else {
-      const errorMessage = `Authentication failed. Response data: ${resp.data.libelleRetour}`
-      log('debug', errorMessage)
-      throw new Error(`code retour ko : ${resp.data.codeRetour}`)
     }
+    const errorMessage = `Authentication failed. Response data: ${resp?.data?.libelleRetour}`
+    log('error', errorMessage)
+    throw new Error(errors.VENDOR_DOWN)
   } catch (error) {
-    log('debug', error.message)
+    log('error', error.response?.data)
     Sentry.captureException(error, {
       tags: {
         section: 'authenticate',
@@ -234,7 +234,10 @@ async function authenticate(login, password, baseUrl, apiAuthKey) {
         compte: login,
       },
     })
-    throw new Error(errors.LOGIN_FAILED)
+    if (error.response?.data.codeRetour === -4) {
+      throw new Error(errors.LOGIN_FAILED)
+    }
+    throw new Error(errors.VENDOR_DOWN)
   }
 }
 
-- 
GitLab