admin管理员组

文章数量:1393908

Im using the async function inside the object to send a response in express.js

Controller Code :

module.exports = {

    async signUpEmail(req, res) {

        /**
         * @description Parameters from body
         * @param {string} firstName - First Name
         * @inner
         */  

        const firstName = req.body.firstName;

        res.send({ success: name });
        throw new Error(); // purposely Done
    }
}

Question:

Since the signUpEmail method is async in my case and it will get rejected with whatever my async method throw's here it's es Error.(purposely put there)

so getting this logged in the console.

(node:13537) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error
(node:13537) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

So i'm supposed to handle it from the routes from where i'm calling it.

Router Code

    const routes = require('express').Router();
const SignUpController = require('../controllers/signUpController')

// /signup
routes.post('/', SignUpController.signUpEmail);

module.exports = routes;

some what like this SignUpController.signUpEmail().then(…); But since i'm not calling function in the routes i'm just passing. How this can be done effectively ?

PS:Please Don't suggest too plicated solutions. I'm beginner with JS and is learning through.

I Didn't use chainable route handlers because i want to create modular, mountable route handler.

Official Doc Example

Im using the async function inside the object to send a response in express.js

Controller Code :

module.exports = {

    async signUpEmail(req, res) {

        /**
         * @description Parameters from body
         * @param {string} firstName - First Name
         * @inner
         */  

        const firstName = req.body.firstName;

        res.send({ success: name });
        throw new Error(); // purposely Done
    }
}

Question:

Since the signUpEmail method is async in my case and it will get rejected with whatever my async method throw's here it's es Error.(purposely put there)

so getting this logged in the console.

(node:13537) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error
(node:13537) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

So i'm supposed to handle it from the routes from where i'm calling it.

Router Code

    const routes = require('express').Router();
const SignUpController = require('../controllers/signUpController')

// /signup
routes.post('/', SignUpController.signUpEmail);

module.exports = routes;

some what like this SignUpController.signUpEmail().then(…); But since i'm not calling function in the routes i'm just passing. How this can be done effectively ?

PS:Please Don't suggest too plicated solutions. I'm beginner with JS and is learning through.

I Didn't use chainable route handlers because i want to create modular, mountable route handler.

Official Doc Example

Share Improve this question edited Jun 30, 2017 at 23:49 iehrlich 3,5804 gold badges35 silver badges43 bronze badges asked Apr 22, 2017 at 20:25 Ankur AnandAnkur Anand 3,9042 gold badges26 silver badges45 bronze badges 7
  • Just pass a function that does: routes.pos('/', (...args) => SignUpController.signUpEmail().then(…)) – Bergi Commented Apr 22, 2017 at 21:26
  • @Bergi getting this now TypeError: Cannot read property 'body' of undefined. seems spreads not passing req ..tried console.log getting undefined – Ankur Anand Commented Apr 22, 2017 at 21:42
  • Guess you'll want …signUpEmail(...args)… or whatever your function expects – Bergi Commented Apr 22, 2017 at 21:51
  • @Bergi yes got it figured.. if You could give a little enlightenment why this way its works especially how (...args) => gets available and what is this ? Because if i do` routes.post('/', console.log(args));` i get this ReferenceError: args is not defined would be very much helpful – Ankur Anand Commented Apr 22, 2017 at 22:05
  • 1 FYI, async/await is not part of ES7, it's part of ES2017. – Felix Kling Commented Apr 23, 2017 at 17:02
 |  Show 2 more ments

1 Answer 1

Reset to default 8

In your route, you will need to add a wrapper to catch thrown errors:

let wrapper = fn => (...args) => fn(...args).catch(args[2]);

// /signup
routes.post('/', wrapper(SignUpController.signUpEmail));

With this method you can use a top level error catcher and do not need to use internal try catch blocks in your routes, unless you need to them contextually.

Use error catching middleware to achieve this as follows:

// last middleware in chain

app.use(function(err, req, res, next) {
    // handle your errors
});

Now in your routes you can throw errors and they will be caught by that middleware. I like to throw custom errors and handle their response and logging in this middleware.

Aside: The async await pattern is great for writing asynchronous code that is easy for a human to read in a synchronous fashion. Just remember that once you go async, you should stay async! Using a promisification library such as Bluebird and invoking .promisifyAll on nodeback libraries is highly remended.

EDIT : Source - Asynchronous Error Handling in Express with Promises, Generators and ES7

本文标签: