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

Commit 90723f1

Browse files
author
Fifciuu
committed
fix: apiError instead of throws
1 parent 51f9e33 commit 90723f1

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/api/catalog.ts

Lines changed: 21 additions & 5 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,7 +57,9 @@ 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
}
@@ -69,17 +74,28 @@ export default ({config, db}) => async function (req, res, body) {
6974

7075
let indexName = ''
7176
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 {
77+
if (urlSegments.length < 2) {
78+
const errMessage = 'No index name given in the URL. Please do use following URL format: /api/catalog/<index_name>/<entity_type>_search';
79+
console.error(errMessage);
80+
apiError(res, errMessage);
81+
return;
82+
} else {
7383
indexName = urlSegments[1]
7484

7585
if (urlSegments.length > 2) { entityType = urlSegments[2] }
7686

7787
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')
88+
const errMessage = 'Invalid / inaccessible index name given in the URL. Please do use following URL format: /api/catalog/<index_name>/_search';
89+
console.error(errMessage);
90+
apiError(res, errMessage);
91+
return;
7992
}
8093

8194
if (urlSegments[urlSegments.length - 1].indexOf('_search') !== 0) {
82-
throw new Error('Please do use following URL format: /api/catalog/<index_name>/_search')
95+
const errMessage = 'Please do use following URL format: /api/catalog/<index_name>/_search';
96+
console.error(errMessage);
97+
apiError(res, errMessage);
98+
return;
8399
}
84100
}
85101

0 commit comments

Comments
 (0)