How to handle async callback promise rejection? - javascript

Try to handle every exception in my async code (nodeJS, ExpressJS):
Here is almost pseudo code. I use limiter (npm limiter) module with method removeTokens (num, callback(err,remainingRequest)). Big part of code is inside the callback, and I wanna catch and throw any error there to the handler, but for now the error inside callback is still marked as "unhandled exception" and I don't understand why.
app.post('/', async (req, res) => {
try {
...
return getAll();
async function getAll () {
limiter.removeTokens(1, async (err, remainingRequest) => {
try {
throw new Error('THROWN')
} catch (error) {
throw error
}
})
}
} catch (error) {
console.log(error);
}
});

You shouldn't pass async functions into things that don't expect them (unless you catch all errors, as you are with your app.post callback). Instead, give yourself a wrapper for limiter.removeTokens that returns a promise:
function removeTokens(limiter, id) {
return new Promise((resolve, reject) => {
limiter.removeTokens(id, (err, remainingRequest) => {
if (err) {
reject(err);
} else {
resolve(remainingRequest);
}
});
});
}
(You might also look into util.promisify for that.)
Then:
app.post('/', async (req, res) => {
try {
...
await getAll(); // *** Or you might just use `removeTokens(limiter, 1)` directly here
function getAll() {
return removeTokens(limiter, 1);
}
} catch (error) {
console.log(error);
}
});
Here it is using removeTokens directly:
app.post('/', async (req, res) => {
try {
...
await removeTokens(limiter, 1);
} catch (error) {
console.log(error);
}
});

Firstly if possible please share as much code as you can as then it is easy for us to debug where the problem might be.
Coming you your question i think the problem is that in your try..catch block you are throwing the error instead of handling it with a reject. Below i have pasted a code block which you can try and let me know if it works for you. Please not the syntax might be different but the idea is that you have to reject the Promise in case of error.
`````````limiter.removeTokens(1, async (err, remainingRequest) => {
````````````try {
```````````````throw new Error('THROWN')
````````````} catch (error) {
```````````````return Promise.reject(error) //
````````````}
`````````})
``````}
```} catch (error) {
``````console.log(error);
```}
})

Related

Catching Errors to Fix Promise Rejections

I was getting some unhandled promise rejection errors, and am trying to catch them and console them after each .then statement
However, I am getting an 'unexpected token' when trying to run on my server with the period at .try
I tried different formats and tried removing the try in general but to no avail.
Can someone provide some tips on how to avoid a syntax error in this case?
app.get('/:url', async function (req, res) {
.init().then(async function() {
.try {
} catch (error) {
}(err => console.log(err)) ;
.open(decodeURIComponent(req.query.url)).then((site) => {
site.analyze().then(async function(results) {
.try {
} catch (error) {
}(err => console.log(err));
res.json(results);
For additional context: This is an express application.
The .try is not really attached to anything, because it is in the callback of your .then() function. I don't think you need a try at all, you can just do something like
app.get('/:url', async function (req, res) {
wappalyzer.init().then(async function() {
// do something on success
})
.catch((err) => {
console.error(err)
})
})
Your code is ambiguous, see comment. But my guess is that what you are looking for is the proper way to handle exceptions in the promises you are currently handling the resolve case of (then), but not yet the rejections, in which case this might be what you are looking for:
app.get('/:url', async function (req, res) {
wappalyzer.init().then(function() {
// handle success ...
}).catch(err => console.log(err));
wappalyzer.open(decodeURIComponent(req.query.url)).then((site) => {
site.analyze().then(function(results) {
// handle success ...
}).catch(err => console.log(err));
res.json(results);
})
}

Why isn't try catch blocks catching exceptions

So I have this piece of code that works fine, but if there is an error with the data type the try catch blocks are not working. It appears I am going to have to check all data types before using them to ensure my server doesn't crash.
For instance in the following code, If tokens for some reason wasn't an array, when the error get's thrown, my try catch isn't catching the error. Instead it is crashing the server.
router.patch("/updateDeviceToken", checkIfAuthenticated, (req, res) => {
try {
console.log("In updating contacts");
console.log("body", req.body);
User.findById(req.authId, (error, user) => {
try {
if (error) {
console.log(error);
console.log("user found");
return res.status(404).json(error);
}
if (!user) return res.status(401).json("User not found");
if (
req.body.android &&
!user.tokens.android.includes(req.body.android)
) {
user.tokens.android.push(req.body.android);
user
.save()
.then((user) => {
console.log("token updated");
return res.json("Token updated");
})
.catch((error) => res.status(404).json(error));
} else if (req.body.iOS && !user.tokens.iOS.includes(req.body.iOS)) {
user.tokens.iOS.push(req.body.iOS);
user
.save()
.then((user) => {
console.log("token updated");
return res.json("Token updated");
})
.catch((error) => res.status(404).json(error));
} else {
console.log("token received but not updated");
return res.json("Token received but not updated");
}
} catch (e) {
console.log(e);
return res.status(404).json("There was an error");
}
});
} catch (e) {
console.log(e);
}
});
try catch doesnt work well with then
Can you change code to use promises with await / async ?
You can use try catch with it
I struggled with that as it is easily overlooked in the documentation but For errors returned from asynchronous functions invoked by route handlers and middleware, you must pass them to the next() function, where Express will catch and process them. For example:
app.get('/', function (req, res, next) {
fs.readFile('/file-does-not-exist', function (err, data) {
if (err) {
next(err) // Pass errors to Express.
} else {
res.send(data)
}
})
})
You can also write your custom error handler, long read but it will save you a lot of time.

Stop Execution of Code After Error thrown in Async Await function

I am creating a Nodejs and express based backend application and trying to handle error in a manner which is suitable for production systems.
I use async await to handle all synchronous operations in the code.
Here is a code snippet of router end points
app.get("/demo",async (req, res, next) => {
await helper().catch(e => return next(e))
console.log("After helper is called")
res.json(1)
})
function helper(){ //helper function that throws an exception
return new Promise((resolve, reject)=> reject(new Error("Demo Error")))
}
After all routes are defined I have added a common error handler that catches exceptions. To simplify it I am adding a simple function
routes.use( (err, req, res, next) => {
console.log("missed all", err)
return res.status(500).json({error:err.name, message: err.message});
});
I expect that the code after await helper() should not execute since the exception has been handled and response sent to frontend. Instead what I get is this error.
After helper is called
(node:46) UnhandledPromiseRejectionWarning: Error
[ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the
client
What is the correct way to handle error with async await?
You get After helper is called, because your code continues to execute since it did not return
Don't chain catch with async/await. You do that with Promise.
helper()
.then(data => console.log(data))
.catch(e => console.log(e))
You can handle error like:
app.get("/demo",async (req, res, next) => {
try {
await helper();
// respond sent if all went well
res.json(something)
catch(e) {
// don't need to respond as you're doing that with catch all error handler
next(e)
}
})
you can use try catch to handle the situation
app.get("/demo",async (req, res, next) => {
try {
await helper()
console.log("After helper is called")
res.json(1)
} catch(err) {
next(err)
}
})
function helper(){ //helper function that throws an exception
return new Promise((resolve, reject)=> reject(new Error("Demo Error")))
}

How to throw exception for sync callback in nodejs?

I want to throw an error exception for an redis.set callback and catch in try-catch block and then get control to error handling express middleware.
try {
redis.get('key', (err, reply) => {
if(err) throw err;
if(!reply) throw new Error('Can't find key');
});
}
catch{
next(error);
}
the problem is, that try-catch is simply not working, error is going to node console, but server is responding with 200 status.
You cant catch async events. Use promises for that:
const getKey = new Promise((res,rej) => {
redis.get('key', (err, reply) => {
if(err) return rej(err);
res(reply);
});
});
So one can do:
getKey.catch(next);
getKey.then(reply => {
//do whatever
next();
});

Handling Errors (Rejections) in async/await inside Array#map

Node 8.1.2, I have a structure where one file is calling another file's function in a map. In a real example I would use Promise.all on the map but that's not the question here. Here is the structure:
A.js:
const { b } = require('./B')
function expressStuff (req, res, next) {
things.map(thing => {
return b(thing)
}))
return res.status(200).json(...)
}
B.js:
// Thing -> Promise<Object>
function b (thing) {
return ThingModel.update(...) // this returns a Promise but FAILS and throws an errror
}
module.exports = { b }
OK. So in function b I try to get some async data (from a database). It fails and throws an Uncaught Promise Rejection.
How to make deal with it?
I tried multiple solutions:
A1.js:
const { b } = require('./B')
function expressStuff (req, res, next) {
things.map(thing => {
try {
return b(thing)
} catch (err) {
return next(err)
}
}))
return res.status(200).json(...)
}
But that is still uncaught.
A2.js:
const { b } = require('./B')
function expressStuff (req, res, next) {
try {
things.map(thing => {
return b(thing)
}))
} catch (err) {
return next(err)
}
return res.status(200).json(...)
}
Still unhandled. I tried using Promise.all, I tried double try-catch blocks (since I thought the one inside map might be returning next from the to the map result and not actually from expressStuff function. Still nothing.
The closes I got to the answer was handling the error but then code wouldn't wait for it to be thrown and both res.status() and next would work resulting in race conditions and cannot set headers after they are sent errors.
All I want to do is for the function b to throw an error but catch it in the expressStuff so I can rethrow custom UnprocessableEntityError and pass it to next. It seems like error from file B is not bubbling up to the map where it is called.
How do I do it?
EDIT:
The only way I can make this rejection handled is try-catching it in the B.js. But if I try to rethrow an error/return it - nothing. Error is swallowed. If I try to console.log it - it will be logged though.
DETAILS:
Thanks to marked answer I refactored my actual code and made it to work perfectly.
function expressStuff (res, req, next) {
try {
await Promise.all(things.map(async thing => {
if (ifSomething()) {
await b(thing)
}
}))
} catch (err) {
return next(new MyCustomError('My Custom Error Message'))
}
return res.status(200).json(...)
}
Handling rejections with try/catch works only in async functions when you await the promise - which you haven't attempted yet.
You could do either
async function expressStuff (req, res, next) {
var results;
try {
results = await Promise.all(things.map(b)); // throws when any of the promises reject
} catch (err) {
return next(err) // handle error
}
return res.status(200).json(...)
}
or (like Wait until all ES6 promises complete, even rejected promises)
function expressStuff (req, res, next) {
const resultPromises = things.map(async (thing) => {
try {
return await b(thing); // throws when the promise for this particular thing rejects
} catch (err) {
return defaultValue; // handle error - don't call `next` here
}
});
…
return res.status(200).json(...)
}

Categories

Resources