From 80fa7fe57c2e8021a2970f0077df27711e54fbf6 Mon Sep 17 00:00:00 2001
From: Nataliia Radina <nradina@gitlab.com>
Date: Mon, 19 Feb 2024 12:01:36 +0100
Subject: [PATCH] Show more specific error messages

Display error messages coming from server when possible

Changelog: changed
---
 .../projects/forks/new/components/fork_form.vue  |  7 ++++---
 .../forks/new/components/fork_form_spec.js       | 16 +++++++++++++++-
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/app/assets/javascripts/pages/projects/forks/new/components/fork_form.vue b/app/assets/javascripts/pages/projects/forks/new/components/fork_form.vue
index bfa2f2cc14ff..a6c31faa45db 100644
--- a/app/assets/javascripts/pages/projects/forks/new/components/fork_form.vue
+++ b/app/assets/javascripts/pages/projects/forks/new/components/fork_form.vue
@@ -283,10 +283,11 @@ export default {
         redirectTo(data.web_url); // eslint-disable-line import/no-deprecated
       } catch (error) {
         this.isSaving = false;
+        const message =
+          error?.response?.data?.message?.join('. ') ||
+          s__('ForkProject|An error occurred while forking the project. Please try again.');
         createAlert({
-          message: s__(
-            'ForkProject|An error occurred while forking the project. Please try again.',
-          ),
+          message,
         });
       }
     },
diff --git a/spec/frontend/pages/projects/forks/new/components/fork_form_spec.js b/spec/frontend/pages/projects/forks/new/components/fork_form_spec.js
index b0bfa4620c6c..532ad52b37e6 100644
--- a/spec/frontend/pages/projects/forks/new/components/fork_form_spec.js
+++ b/spec/frontend/pages/projects/forks/new/components/fork_form_spec.js
@@ -578,7 +578,21 @@ describe('ForkForm component', () => {
         expect(urlUtility.redirectTo).toHaveBeenCalledWith(webUrl); // eslint-disable-line import/no-deprecated
       });
 
-      it('displays an alert when POST is unsuccessful', async () => {
+      it('displays an alert with message coming from server when POST is unsuccessful', async () => {
+        const error = { response: { data: { message: ['Update error'] } } };
+
+        jest.spyOn(axios, 'post').mockRejectedValue(error);
+
+        setupComponent();
+        await submitForm();
+
+        expect(urlUtility.redirectTo).not.toHaveBeenCalled(); // eslint-disable-line import/no-deprecated
+        expect(createAlert).toHaveBeenCalledWith({
+          message: 'Update error',
+        });
+      });
+
+      it('displays an alert with general error when POST is unsuccessful', async () => {
         const dummyError = 'Fork project failed';
 
         jest.spyOn(axios, 'post').mockRejectedValue(dummyError);
-- 
GitLab