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

Commit 1e62369

Browse files
authored
Merge pull request #543 from vuestorefront/apiresponse-ibstead-of-throw-master
apiError instead of throws
2 parents 51f9e33 + 1adc7e4 commit 1e62369

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [1.12.4] - 2021.01.15
8+
9+
### Fixed
10+
11+
- Responsing with error instead of throwing for broken /api/catalog paths - @Fifciu
12+
713
## [1.12.3] - 2020.07.23
814

915
### Fixed
1016

1117
- Bump version for `vsf-utilities` - @gibkigonzo (#495)
1218

13-
1419
## [1.12.2] - 2020.07.20
1520

1621
### Added

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-storefront-api",
3-
"version": "1.12.3",
3+
"version": "1.12.4",
44
"private": true,
55
"description": "vue-storefront API and data services",
66
"main": "dist",

src/api/catalog.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ export default ({config, db}) => async function (req, res, body) {
4444
// Request method handling: exit if not GET or POST
4545
// Other methods - like PUT, DELETE etc. should be available only for authorized users or not available at all)
4646
if (!(req.method === 'GET' || req.method === 'POST' || req.method === 'OPTIONS')) {
47-
throw new Error('ERROR: ' + req.method + ' request method is not supported.')
47+
const errMessage = 'ERROR: ' + req.method + ' request method is not supported.';
48+
console.error(errMessage);
49+
apiError(res, errMessage);
50+
return;
4851
}
4952

5053
let responseFormat = 'standard'
@@ -54,32 +57,51 @@ export default ({config, db}) => async function (req, res, body) {
5457
try {
5558
requestBody = JSON.parse(decodeURIComponent(req.query.request))
5659
} catch (err) {
57-
throw new Error(err)
60+
console.error(err);
61+
apiError(res, err);
62+
return;
5863
}
5964
}
6065
}
6166

6267
if (req.query.request_format === 'search-query') { // search query and not Elastic DSL - we need to translate it
63-
const customFilters = await loadCustomFilters(config)
64-
requestBody = await elasticsearch.buildQueryBodyFromSearchQuery({ config, queryChain: bodybuilder(), searchQuery: new SearchQuery(requestBody), customFilters })
68+
try {
69+
const customFilters = await loadCustomFilters(config)
70+
requestBody = await elasticsearch.buildQueryBodyFromSearchQuery({ config, queryChain: bodybuilder(), searchQuery: new SearchQuery(requestBody), customFilters })
71+
} catch (err) {
72+
console.error(err);
73+
apiError(res, err);
74+
return;
75+
}
6576
}
6677
if (req.query.response_format) responseFormat = req.query.response_format
6778

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

7081
let indexName = ''
7182
let entityType = ''
72-
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 {
83+
if (urlSegments.length < 2) {
84+
const errMessage = 'No index name given in the URL. Please do use following URL format: /api/catalog/<index_name>/<entity_type>_search';
85+
console.error(errMessage);
86+
apiError(res, errMessage);
87+
return;
88+
} else {
7389
indexName = urlSegments[1]
7490

7591
if (urlSegments.length > 2) { entityType = urlSegments[2] }
7692

7793
if (config.elasticsearch.indices.indexOf(indexName) < 0) {
78-
throw new Error('Invalid / inaccessible index name given in the URL. Please do use following URL format: /api/catalog/<index_name>/_search')
94+
const errMessage = 'Invalid / inaccessible index name given in the URL. Please do use following URL format: /api/catalog/<index_name>/_search';
95+
console.error(errMessage);
96+
apiError(res, errMessage);
97+
return;
7998
}
8099

81100
if (urlSegments[urlSegments.length - 1].indexOf('_search') !== 0) {
82-
throw new Error('Please do use following URL format: /api/catalog/<index_name>/_search')
101+
const errMessage = 'Please do use following URL format: /api/catalog/<index_name>/_search';
102+
console.error(errMessage);
103+
apiError(res, errMessage);
104+
return;
83105
}
84106
}
85107

0 commit comments

Comments
 (0)