diff --git a/assets/src/bundles/add_forge/create-request.js b/assets/src/bundles/add_forge/create-request.js --- a/assets/src/bundles/add_forge/create-request.js +++ b/assets/src/bundles/add_forge/create-request.js @@ -23,11 +23,32 @@ $('#userMessage').removeClass('badge-danger'); $('#userMessage').addClass('badge-success'); requestBrowseTable.draw(); // redraw the table to update the list - } catch (response) { - const responseText = await response.json(); + } catch (errorResponse) { $('#userMessageDetail').empty(); - $('#userMessage').text('Sorry; an error occurred'); - $('#userMessageDetail').text(responseText.substring(0, 500)); + + let errorMessage; + let errorMessageDetail = ''; + const errorData = await errorResponse.json(); + // if (errorResponse.content_type === 'text/plain') { // does not work? + if (errorResponse.status === 409) { + errorMessage = errorData; + } else { // assuming json response + // const exception = errorData['exception']; + errorMessage = 'An unknown error occurred during the request creation'; + try { + const reason = JSON.parse(errorData['reason']); + Object.entries(reason).forEach((keys, _) => { + const key = keys[0]; + const message = keys[1][0]; // take only the first issue + errorMessageDetail += `\n${key}: ${message}`; + }); + } catch (_) { + errorMessageDetail = errorData['reason']; // can't parse it, leave it raw + } + } + $('#userMessage').text( + errorMessageDetail ? `Error: ${errorMessageDetail}` : errorMessage + ); $('#userMessage').removeClass('badge-success'); $('#userMessage').addClass('badge-danger'); } diff --git a/cypress/integration/add-forge-now-request-create.spec.js b/cypress/integration/add-forge-now-request-create.spec.js --- a/cypress/integration/add-forge-now-request-create.spec.js +++ b/cypress/integration/add-forge-now-request-create.spec.js @@ -134,6 +134,33 @@ cy.get('#userMessage') .should('have.class', 'badge-danger') - .should('contain', 'Sorry; an error occurred'); + .should('contain', 'already exists'); }); + + it('should show error message', function() { + cy.userLogin(); + + cy.intercept('POST', `${this.Urls.api_1_add_forge_request_create()}**`, + { + body: { + 'exception': 'BadInputExc', + 'reason': '{"add-forge-comment": ["This field is required"]}' + }, + statusCode: 400 + }).as('errorRequest'); + + cy.visit(this.addForgeNowUrl); + + populateForm( + 'bitbucket', 'gitlab.com', 'test', 'test@example.com', 'on', 'comment' + ); + cy.get('#requestCreateForm').submit(); + + cy.wait('@errorRequest').then((xhr) => { + cy.get('#userMessage') + .should('have.class', 'badge-danger') + .should('contain', 'field is required'); + }); + }); + });