So basically I am trying to create an Error handler by passing an error value which in my case is a number for a status code like 400, 500, etc.
Now the problem is when the catch
catches the error, which contains a value of the status code returned from the previous promise. When I log the value it logs the value correctly, but when I reject it and pass the value so it will be caught on the next promise's catch
, it says that the value is undefined.
Here's the error code:
UnhandledPromiseRejectionWarning: RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined
JS:
const findFromModel = async (model, query) => {
return new Promise((resolve, reject) => {
model = model instanceof Object ? model : {};
if (model) {
model.findOne(query, (err, obj) => {
if (obj) {
resolve(obj);
} else {
reject(400);
}
});
} else {
reject(400);
}
});
}
const makeOrder = async (title, body, userQuery) => {
return new Promise((resolve, reject) => {
title = (typeof title === 'string' && title.length > 0) ? title : '';
body = (typeof body === 'string' && body.length > 0) ? body : '';
if (title && body) {
findFromModel(userModel, userQuery)
.then(user => resolve(user))
.catch(err => {
console.log(err); // logs 400
reject(err); // Undefined
});
} else {
reject(400);
}
});
}
Execute the promise:
makeOrder('test', 'this is body', {id: 'test'})
.then(obj => res.send(obj))
.catch(err => res.sendStatus(err.message));
I just move from es5 to es6, so if you got any better ideas to improve my code, that will be hugely appreciated.