Skip to content
This repository has been archived by the owner on May 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request #543 from vuestorefront/apiresponse-ibstead-of-thr…
Browse files Browse the repository at this point in the history
…ow-master

apiError instead of throws
  • Loading branch information
Fifciu authored Jan 15, 2021
2 parents 51f9e33 + 1adc7e4 commit 1e62369
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
36 changes: 29 additions & 7 deletions src/api/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -54,32 +57,51 @@ 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

const urlSegments = req.url.split('/')

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/<index_name>/<entity_type>_search') } else {
if (urlSegments.length < 2) {
const errMessage = 'No index name given in the URL. Please do use following URL format: /api/catalog/<index_name>/<entity_type>_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/<index_name>/_search')
const errMessage = 'Invalid / inaccessible index name given in the URL. Please do use following URL format: /api/catalog/<index_name>/_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/<index_name>/_search')
const errMessage = 'Please do use following URL format: /api/catalog/<index_name>/_search';
console.error(errMessage);
apiError(res, errMessage);
return;
}
}

Expand Down

0 comments on commit 1e62369

Please sign in to comment.