Search code examples
node.jsexpresshttp-status-code-403http-status-code-500node-acl

NODE ACL Module generating 500 instead of 403


I am using Node_ACL, for authorization purpose it is working fine but whenever any request gets denied it generates 500 Internal Server Error but it should show 403 Access denied error. I already looked into its proper installation and setting but I didn't find any solution.

I am using it with Express js as a middleware. I am not sure who is causing the 500 Internal Server Error. I mean I have used Node-ACL as a middleware in Express js. Whenever a client makes any request the Node ALC authenticate it first and then give access to the particular resource. So when the authorization gets failed, the 500 Internal Server Error is coming to the client from the server. So Express can also be the reason because it's middleware is getting failed. I am not sure what to do.

Please help. Thanks.


Solution

  • Now I got a solution, the Node-ACL was working properly. We just need to add some code to our Express app.js (server) file. The code is:

    // Error Handler
    app.use(function(err, req, res, next) {
        res.status(err.status || 500).send({message: err.message});
    });
    

    Add this code at the end of the file (just before module.exports)

    Now I am getting a proper error code.