Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't pass error to middleware handler when using async functions in controller #15

Open
cristianogomes opened this issue Aug 18, 2019 · 1 comment

Comments

@cristianogomes
Copy link

A controller with an async function that throws an error, that error doesn't reach the error handler middleware.

cars-get.action.js

module.exports.getAll = async (req, res) => {
  throw new Error('async error');
};

express.js

const express = require('express');
const Lumie = require('lumie');
const app = express();

Lumie.load(app, {
  verbose: process.env.NODE_ENV !== 'test',
  preURL: 'api',
  ignore: ['*.spec', '*.action'],
  controllers_path: path.join(__dirname, '../api/controllers')
});

//error handler middleware
app.use((err, req, res, next) => {
  return res.status(500).json({ message: 'error' });
});

module.exports = app;
(node:3690) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:3690) [DEP0018] 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.

Sugestion

Use a function to wrap the controller action that resolve the promise, in case of error, catch that and pass it to next middleware

https://stackoverflow.com/questions/51391080/handling-errors-in-express-async-middleware

@Alex-Levacher
Copy link
Owner

Hi @cristianogomes, thanks for reporting this, I wasn't aware of this behavior. I'm not sure how to implement it right now, I have to think about it. Should I set a new param in the route definition ?

get: {
  action: asyncHandler(getCars.getAll),
  level: 'public',
  async: true
}

Or use an handler for every actions ? Or every actions that return a Promise.

However, I suggest you to create an "asyncHandler" and use it in the routing definitions where needed.

const postCars = require('./cars-post.action');
const getCars = require('./cars-get.action');

const asyncHandler = fn => (req, res, next) => Promise.resolve(fn(req, res)).catch(next);

module.exports = {
    '/': {
        post: {
            middlewares: postCars.middlewares,
            action: asyncHandler(postCars.action),
            level: 'public'
        },
        get: {
            action: asyncHandler(getCars.getAll),
            level: 'public'
        }
    },
    '/:id': {
        get: {
            action: getCars.getOne,
            level: 'public'
        }
    }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants