diff --git a/CHANGELOG.md b/CHANGELOG.md index 09a696f3..c872a84c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,13 +4,18 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.12.4] - 2021.01.15 + +### Fixed + +- Responsing with error instead of throwing for broken /api/catalog paths - @Fifciu + ## [1.12.3] - 2020.07.23 ### Fixed - Bump version for `vsf-utilities` - @gibkigonzo (#495) - ## [1.12.2] - 2020.07.20 ### Added diff --git a/package.json b/package.json index 62a6de48..b0862232 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue-storefront-api", - "version": "1.12.3", + "version": "1.12.4", "private": true, "description": "vue-storefront API and data services", "main": "dist", diff --git a/src/api/catalog.ts b/src/api/catalog.ts index 0f7128f4..86471645 100755 --- a/src/api/catalog.ts +++ b/src/api/catalog.ts @@ -44,7 +44,10 @@ export default ({config, db}) => async function (req, res, body) { // Request method handling: exit if not GET or POST // Other methods - like PUT, DELETE etc. should be available only for authorized users or not available at all) if (!(req.method === 'GET' || req.method === 'POST' || req.method === 'OPTIONS')) { - throw new Error('ERROR: ' + req.method + ' request method is not supported.') + const errMessage = 'ERROR: ' + req.method + ' request method is not supported.'; + console.error(errMessage); + apiError(res, errMessage); + return; } let responseFormat = 'standard' @@ -54,14 +57,22 @@ export default ({config, db}) => async function (req, res, body) { try { requestBody = JSON.parse(decodeURIComponent(req.query.request)) } catch (err) { - throw new Error(err) + console.error(err); + apiError(res, err); + return; } } } if (req.query.request_format === 'search-query') { // search query and not Elastic DSL - we need to translate it - const customFilters = await loadCustomFilters(config) - requestBody = await elasticsearch.buildQueryBodyFromSearchQuery({ config, queryChain: bodybuilder(), searchQuery: new SearchQuery(requestBody), customFilters }) + try { + const customFilters = await loadCustomFilters(config) + requestBody = await elasticsearch.buildQueryBodyFromSearchQuery({ config, queryChain: bodybuilder(), searchQuery: new SearchQuery(requestBody), customFilters }) + } catch (err) { + console.error(err); + apiError(res, err); + return; + } } if (req.query.response_format) responseFormat = req.query.response_format @@ -69,17 +80,28 @@ export default ({config, db}) => async function (req, res, body) { let indexName = '' let entityType = '' - if (urlSegments.length < 2) { throw new Error('No index name given in the URL. Please do use following URL format: /api/catalog//_search') } else { + if (urlSegments.length < 2) { + const errMessage = 'No index name given in the URL. Please do use following URL format: /api/catalog//_search'; + console.error(errMessage); + apiError(res, errMessage); + return; + } else { indexName = urlSegments[1] if (urlSegments.length > 2) { entityType = urlSegments[2] } if (config.elasticsearch.indices.indexOf(indexName) < 0) { - throw new Error('Invalid / inaccessible index name given in the URL. Please do use following URL format: /api/catalog//_search') + const errMessage = 'Invalid / inaccessible index name given in the URL. Please do use following URL format: /api/catalog//_search'; + console.error(errMessage); + apiError(res, errMessage); + return; } if (urlSegments[urlSegments.length - 1].indexOf('_search') !== 0) { - throw new Error('Please do use following URL format: /api/catalog//_search') + const errMessage = 'Please do use following URL format: /api/catalog//_search'; + console.error(errMessage); + apiError(res, errMessage); + return; } }