@@ -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