I'm trying to parse the error from the Elasticsearch client called through a nodeJs express routes. I my case it is the timeout error I would like to identify.
I more or less implement something like that:
var client = new elasticsearch.Client( {
host: process.env.ELASTICSEARCHLOCATION,
requestTimeout:500,
maxRetries:1
});
function returnResponse( response ) {
return function ( resp ) {
//stringExtract
if ( resp.hits && resp.hits.hits ) {
response.status( 200 ).send( resp.hits.hits );
}else{
response.status( 200 ).send( [] );
}
};
}
function handleError( response ) {
return function( err ){
if ( err.statusCode == 404 ) {
response.status( 200 ).send( [] );
} else {
console.error( err.message );
response.status( err.statusCode ).send( err );
}
}
}
router.get( "/test", function ( req, res, next ) {
log.data( "route GET: ", "/wynsureSearch/test" );
log.request( req.url );
client .search( {
index: process.env.ELASTICSEARCHINDEX, //target the wynsure version aka the ES DB index
type: [], //target the wynsure types
body: req.body
}).then( returnResponse( res ), handleError( res ) );
})
All works fine when there are no errors. But when a timeout error occurs, the breakpoint in the function returned by handleError()
shows me this:
I read from elastic.co errors documentation that there are standars errors. How I have my function match matching the errors from documentation? I would like to be able to match RequestTimeout or InternalServerError for example.
res
parameter passed in returnResponse and handleError refers to the parameter passed in router.get( "/test", function ( req, res, next ) {
therefore it is undefined
.
Quote from Elasticsearch
When a callback is passed to any of the API methods, it will be called with (err, response, status). If you prefer to use promises, don't pass a callback and a promise will be returned. The promise will either be resolved with the response body, or rejected with the error that occured (including any 300+ response for non "exists" methods).
client .search( {
index: process.env.ELASTICSEARCHINDEX, //target the wynsure version aka the ES DB index
type: [], //target the wynsure types
body: req.body }, function callback(err, response, status){
if (err) {
if(status == 404) res.status(200).send();
else console.error(err.message);
}
else res.status(200).send(response); // res variable is refering to HTTP response whereas response is the result returned from elasticsearch.
});
Note: status codes in ElasticSearch have their own meaning, don't confuse them with HTTP status codes.
Note: Javascript supports enclosing scopes, not block scopes.
Concerning the undefined status code, there seems to be a bug with the status code definition in ElasticSearch.JS .
@Spalger offers a solution about how using the JavaScript Elasticsearch client errors?:
// require the Error class
var EsClientRequestTimeout = require('elasticsearch').errors.RequestTimeout;
// ... somewhere else in your code
client.search(..., function (err) {
if (err instanceof EsClientRequestTimeout) {
console.log('timeout')
} else {
console.log('not a timeout')
}
})
Thank you a lot @Spalger for helping!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.