0

I have the following situation, i have a middleware that add a flag when a user is logging

//if some situation happend
req.feathers.isAuthenticated = true;
//else
req.feathers.isAuthenticated = false;
next();

and i have a hook that check if a user is already logged in some services

myService.before({
        create(hook, next) {
            if (!hook.params.isAuthenticated) {
                throw new Error('AccessDenied');
            }
            next();
        }
    });

this work as expected, the problem is on the error handler, when i add a simple error handler to the end of my app

app.use(function (err, req, res, next) {
        if (isDev) {
            console.error(err);
        }
        res.status(err.status || 500);
        res.json(err);
    });

The err object is the entire hook object, that is an instance of Error too

i just want to get the error that a throw before on the hook, i try calling next(err) on the hook but this not work.

Some one can help me with this please?

EDIT

I would like to not delete the hook property on my error handler

stalin
  • 3,442
  • 2
  • 25
  • 25

1 Answers1

0

When you call next(error) you also have to return at the same time so that next isn't called again:

myService.before({
    create(hook, next) {
        if (!hook.params.isAuthenticated) {
            return next(new Error('AccessDenied'));
        }
        next();
    }
});

If that also does not work please file an issue in feathers-hooks.

Daff
  • 43,734
  • 9
  • 106
  • 120