Promise reject Possibly unhandled Error:
.catch
takes a function as parameter however, you are passing it something else. When you don't pass a function to catch, it will silently just fail to do anything. Stupid but that's what ES6 promises do.
Because the .catch
is not doing anything, the rejection becomes unhandled and is reported to you.
Fix is to pass a function to .catch
:
handlers.getArray = function (request, reply) {
myArrayFunction().then(function (a) {
reply(a);
}).catch(function(e) {
reply(hapi.error.notFound('No array')));
});
};
Because you are using a catch all, the error isn't necessarily a No array error. I suggest you do this instead:
function myArrayFunction() {
// new Promise anti-pattern here but the answer is too long already...
return new Promise(function (resolve, reject) {
var a = new Array();
//some operation with a
if (a.length > 0) {
resolve(a);
} else {
reject(hapi.error.notFound('No array'));
}
};
}
}
function NotFoundError(e) {
return e.statusCode === 404;
}
handlers.getArray = function (request, reply) {
myArrayFunction().then(function (a) {
reply(a);
}).catch(NotFoundError, function(e) {
reply(e);
});
};
Which can be further shortened to:
handlers.getArray = function (request, reply) {
myArrayFunction().then(reply).catch(NotFoundError, reply);
};
Also note the difference between:
// Calls the method catch, with the function reply as an argument
.catch(reply)
And
// Calls the function reply, then passes the result of calling reply
// to the method .catch, NOT what you wanted.
.catch(reply(...))
Juan
I have a PhD in computer science from the Institut National de Science Appliquées (INSA), Lyon, France. During my PhD, I contributed and developed novel algorithms for 3D object modeling, visualization and shape statistical analysis. Since my 2014-15 post-doctoral fellowship at the Brigham Women's Hospital, I have used web technologies to develop new visualization tools for medical images and 3D objects in the browser as well as all the logic and infrastructure to handle thousands of images. I have contributed to the development of methods for statistical analysis in clinical studies. The Dental and Craniofacial Bionetwork for Image Analysis DSCI, is a platform to federate clinical and imaging data as well as computational methods for shape statistics. I have supervised the last 3 major releases of the Slicer Craniomaxillo Facial software, as well as the development of plugins: ShapeVariationAnalyzer and Multivariate Functional Shape Data Analysis - MFSDA. Recently, I have started working with Dr. Stringer, in the Ultrasound Fetal Age Machine Learning Initiative (US-FAMLI) which aims to develop a low-cost obstetric ultrasound for use in low- and middle-income countries. The objective is to to use machine learning algorithms to estimate gestational age and make diagnoses without requiring a skilled ultrasonographer. I use state of the art neural network architectures to detect features in the images allowing measurement of fetal biometry structures, thus, predict gestational age.
Updated on July 09, 2022Comments
-
Juan almost 2 years
I have a function that does some operation using an array. I would like to reject it when the array is empty.
As an example
myArrayFunction(){ return new Promise(function (resolve, reject) { var a = new Array(); //some operation with a if(a.length > 0){ resolve(a); }else{ reject('Not found'); } }; }
When the reject operation happens I get the following error. Possibly unhandled Error: Not found
However I have the following catch when the call to myArrayFunction() is made.
handlers.getArray = function (request, reply) { myArrayFunction().then( function (a) { reply(a); }).catch(reply(hapi.error.notFound('No array'))); };
What would be the correct way to reject the promise, catch the rejection and respond to the client?
Thank you.
-
Juan almost 10 yearsThe fix was to pass a function to .catch as you suggested. The second option i.e. .catch(NotFoundError, reply); gives me the following error "A catch filter must be an error constructor or a filter function"
-
Esailija almost 10 years@juan did you implement NotFoundError
-
Juan almost 10 yearsYes, I it was implemented.
-
Juan almost 10 yearshandlers.getArray = function (request, reply) { var name = request.query.tableId; model.getArray(name) .then(function (dataArray) { reply.file(dataArray[0]); }) .catch(NotFoundError, function(e) { reply(e); }); }; function NotFoundError(e) { return e.statusCode === 404; }
-
Esailija almost 10 years@juan can
e
be null or undefined? -
Juan almost 10 yearsNo, I return a message 'No array found'.
-
Moonwalker almost 9 yearsI have a similar problem however I am attaching a proper catch. I tried catch(error => {}) and catch(function(e){}) - the rejection inside the promise is failing all the time with unhandled promise rejection and the rest of the code inside the promise is executed - this is really bad...