Tuesday 17 April 2018

Custom response with aync/await error handling wrapper inside route contollers?

Hello, I wanted to use async/await instead of simple callbacks in my ExpressJS route controllers, and I have error handling wrapper function that wraps async functions to prevent try/catch blocks all the time but I stumbled upon a problems with it.I cannot send custom responses from it? For example I want control what the controller will respond back, also there might be additional logic inside where I want to send custom error messages, like having conditions that can send back different responses, but using this async error wrapper I can only get the generic error (or the custom errors I set up in mongoose models, but I want to have more options in controllers anyway) or as a response I can only get thing which for example MongoDB gives back from "findOne" method.What if I want to find a record in DB and beside record I want to add more things to the response, I can't find a way to do it with async/await. I just await on result and pass it as res.send(result), no flexibility.I am sure there is a solution to this and would like to hear your opinions. :) Thanks!

Submitted April 17, 2018 at 10:36AM by nikola1970

No comments:

Post a Comment