davidbanham / express-async-errors

async/await support for ExpressJS
Other
900 stars 43 forks source link

not working for Node 8.6 #7

Closed uri3000 closed 6 years ago

uri3000 commented 6 years ago

const Layer = require('express/lib/router/layer');

function wrapErrorMiddleware(fn) { return (err, req, res, next) => { const ret = fn.call(this, err, req, res, next);

    if (ret && ret.catch) {
        ret.catch(innerErr => next(innerErr));
    }

    return ret;
};

}

function wrap(fn) { return (req, res, next) => { const ret = fn.call(this, req, res, next);

    if (ret && ret.catch) {
        ret.catch((err) => {
            next(err);
        });
    }

    return ret;
};

}

Object.defineProperty(Layer.prototype, 'handle', { enumerable: true, get() { return this.__handle; }, set(fn) { // Bizarre, but Express checks for 4 args to detect error middleware: https://github.com/expressjs/express/blob/master/lib/router/layer.js if (fn.length === 4) { fn = wrapErrorMiddleware(fn); } else { fn = wrap(fn); }

    this.__handle = fn;
},

});

module.exports = (app) => {

async function errorHandler(err, req, res, next) {
    console.log(err);
    res.status(500);
    res.render('error', {
        message: err.message,
        error: err,
    });

    next(err);
}

...routes that raise exceptions down this file, not getting to the error handling. }

davidbanham commented 6 years ago

Hi @uri3000, what version of express are you using? I wonder if this might be the same issue as #6