From fc690171b732096b0e4b2a94bcc9ffcf395ea765 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <git@minelli.me>
Date: Tue, 26 Mar 2024 21:28:22 +0100
Subject: [PATCH] HttpManager => Display general error only when there is no
 custom one

---
 NodeApp/src/managers/DojoBackendManager.ts | 10 +++++++--
 NodeApp/src/managers/HttpManager.ts        | 24 +++++++++++++---------
 NodeApp/src/shared                         |  2 +-
 3 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/NodeApp/src/managers/DojoBackendManager.ts b/NodeApp/src/managers/DojoBackendManager.ts
index 657df69..5e48221 100644
--- a/NodeApp/src/managers/DojoBackendManager.ts
+++ b/NodeApp/src/managers/DojoBackendManager.ts
@@ -36,15 +36,21 @@ class DojoBackendManager {
                     case DojoStatusCode.ASSIGNMENT_NAME_CONFLICT:
                         spinner.fail(`Assignment creation error: The assignment name already exists. Please choose another name.`);
                         break;
-                    case DojoStatusCode.ASSIGNMENT_CREATION_ERROR:
+                    case DojoStatusCode.ASSIGNMENT_CREATION_GITLAB_ERROR:
+                        spinner.fail(`Assignment creation error: An unknown error occurred while creating the assignment on GitLab (internal error message: ${ error.response?.data?.description ?? 'unknown error' }). Please try again later or contact an administrator.`);
+                        break;
+                    case DojoStatusCode.ASSIGNMENT_CREATION_INTERNAL_ERROR:
                         spinner.fail(`Assignment creation error: An unknown error occurred while creating the assignment (internal error message: ${ error.response?.data?.description ?? 'unknown error' }). Please try again later or contact an administrator.`);
                         break;
                     case DojoStatusCode.MAX_EXERCISE_PER_ASSIGNMENT_REACHED:
                         spinner.fail(`The following users have reached the maximum number of exercise of this assignment : ${ ((error.response.data as DojoBackendResponse<Array<Gitlab.UserSchema>>).data).map(user => user.name).join(', ') }.`);
                         break;
-                    case DojoStatusCode.EXERCISE_CREATION_ERROR:
+                    case DojoStatusCode.EXERCISE_CREATION_INTERNAL_ERROR:
                         spinner.fail(`Exercise creation error: An unknown error occurred while creating the exercise (internal error message: ${ error.response?.data?.description ?? 'unknown error' }). Please try again later or contact an administrator.`);
                         break;
+                    case DojoStatusCode.EXERCISE_CREATION_GITLAB_ERROR:
+                        spinner.fail(`Exercise creation error: An unknown error occurred while creating the exercise on GitLab (internal error message: ${ error.response?.data?.description ?? 'unknown error' }). Please try again later or contact an administrator.`);
+                        break;
                     case DojoStatusCode.GITLAB_TEMPLATE_NOT_FOUND:
                         spinner.fail(`Template not found or access denied. Please check the template ID or url. Also, please check that the template have public/internal visibility or that your and Dojo account (${ ClientsSharedConfig.gitlab.dojoAccount.username }) have at least reporter role to the template (if private).`);
                         break;
diff --git a/NodeApp/src/managers/HttpManager.ts b/NodeApp/src/managers/HttpManager.ts
index ba4b22a..be8ac3b 100644
--- a/NodeApp/src/managers/HttpManager.ts
+++ b/NodeApp/src/managers/HttpManager.ts
@@ -76,16 +76,20 @@ class HttpManager {
 
     private apiAuthorizationError(error: AxiosError, isFromApi: boolean) {
         if ( this.handleAuthorizationCommandErrors && isFromApi && error.response ) {
-            switch ( error.response.status ) {
-                case StatusCodes.UNAUTHORIZED:
-                    this.requestError('Session expired or does not exist. Please login again.');
-                    break;
-                case StatusCodes.FORBIDDEN:
-                    this.requestError('Forbidden access.');
-                    break;
-                default:
-                    this.requestError('Unknown error.');
-                    break;
+            const errorCustomCode = (error.response.data as DojoBackendResponse<unknown> | undefined)?.code ?? error.response.status;
+            
+            if ( errorCustomCode === error.response.status ) {
+                switch ( error.response.status ) {
+                    case StatusCodes.UNAUTHORIZED:
+                        this.requestError('Session expired or does not exist. Please login again.');
+                        break;
+                    case StatusCodes.FORBIDDEN:
+                        this.requestError('Forbidden access.');
+                        break;
+                    default:
+                        this.requestError('Unknown error.');
+                        break;
+                }
             }
         } else {
             this.handleAuthorizationCommandErrors = true;
diff --git a/NodeApp/src/shared b/NodeApp/src/shared
index 47b289f..556a235 160000
--- a/NodeApp/src/shared
+++ b/NodeApp/src/shared
@@ -1 +1 @@
-Subproject commit 47b289f99ef88df993f5977efa6a53e4bd0abe85
+Subproject commit 556a235605ba36ea775cd54da4aee8a5bc03d912
-- 
GitLab