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 @@ -5,7 +5,7 @@ * See top-level LICENSE file for more information */ -import {handleFetchError, csrfPost, +import {handleFetchError, errorMessageFromResponse, csrfPost, getHumanReadableDate} from 'utils/functions'; import userRequestsFilterCheckboxFn from 'utils/requests-filter-checkbox.ejs'; import {swhSpinnerSrc} from 'utils/constants'; @@ -36,28 +36,16 @@ $('#userMessageDetail').empty(); 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 - } + errorMessage = errorMessageFromResponse( + errorData, 'An unknown error occurred during the request creation'); } - $('#userMessage').text( - errorMessageDetail ? `Error: ${errorMessageDetail}` : errorMessage - ); + $('#userMessage').text(errorMessage); $('#userMessage').removeClass('badge-success'); $('#userMessage').addClass('badge-danger'); } diff --git a/assets/src/bundles/browse/origin-search.js b/assets/src/bundles/browse/origin-search.js --- a/assets/src/bundles/browse/origin-search.js +++ b/assets/src/bundles/browse/origin-search.js @@ -5,7 +5,7 @@ * See top-level LICENSE file for more information */ -import {handleFetchError, isArchivedOrigin} from 'utils/functions'; +import {handleFetchError, errorMessageFromResponse, isArchivedOrigin} from 'utils/functions'; const limit = 100; const linksPrev = []; @@ -147,11 +147,13 @@ $('.swh-loading').removeClass('show'); populateOriginSearchResultsTable(data); - } catch (response) { + } catch (errorResponse) { + const errorData = await errorResponse.json(); $('.swh-loading').removeClass('show'); inSearch = false; $('#swh-origin-search-results').hide(); - $('#swh-no-result').text(`Error ${response.status}: ${response.statusText}`); + $('#swh-no-result').text(errorMessageFromResponse( + errorData, 'An unknown error occurred while searching origins')); $('#swh-no-result').show(); } } diff --git a/assets/src/utils/functions.js b/assets/src/utils/functions.js --- a/assets/src/utils/functions.js +++ b/assets/src/utils/functions.js @@ -25,6 +25,21 @@ return responses; } +export function errorMessageFromResponse(errorData, defaultMessage) { + let errorMessage = ''; + 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 + errorMessage += `\n${key}: ${message}`; + }); + } catch (_) { + errorMessage = errorData['reason']; // can't parse it, leave it raw + } + return errorMessage ? `Error: ${errorMessage}` : defaultMessage; +} + export function staticAsset(asset) { return `${__STATIC__}${asset}`; } diff --git a/cypress/integration/origin-search.spec.js b/cypress/integration/origin-search.spec.js --- a/cypress/integration/origin-search.spec.js +++ b/cypress/integration/origin-search.spec.js @@ -237,6 +237,35 @@ .should('exist'); }); + it('should show error messages when using the query language', function() { + cy.adminLogin(); + cy.visit(url); + + cy.intercept('GET', `${this.Urls.api_1_origin_search('**')}**`, + { + body: { + 'exception': 'BadInputExc', + 'reason': 'Syntax error in search query: Invalid query' + }, + statusCode: 400 + }) + .as('searchOrigin'); + + cy.get('#swh-search-use-ql') + .should('exist') + .click({force: true}); // Covered by label + + cy.get('#swh-origins-url-patterns') + .type('this is not a valid query') + .type('{enter}'); + + cy.wait('@searchOrigin').then((xhr) => { + cy.get('#swh-no-result') + .should('contain', 'Syntax error in search query'); + }); + + }); + context('Test pagination', function() { it('should not paginate if there are not many results', function() { // Setup search