How to make express server to catch JWT expiry error without stopin the server?

Discussão em 'Angular' iniciado por SolisQuid, Dezembro 29, 2024.

  1. SolisQuid

    SolisQuid Guest

    I have an angular app running on some port and an express server running on another port. Both running locally on my laptop.

    I'm testing my api by sending requests from angular to express and I'm having issues with catching errors when JWT token is expired.

    exports.someFunction = async (req, res, next) => {
    if(!jwt.verify(payload, SECRET)) {
    return res.status(400).json('someError');
    }

    jwt.verify(payload, SECRET, async function(err, decoded) {
    if (err) {
    return res.status(400).json('someError');
    }
    else {
    return res.status(200).json('someResponse');
    }
    })
    }


    Now instead of sending an error to my angular app, like it does for instance in case of failed db query with the same error code, it immediately breaks the express listening process with expiry error:


    TokenExpiredError: jwt expired
    ​

    I need to be able to catch that error now so I know it won't be a problem after it's deployed on a production server.

    Continue reading...

Compartilhe esta Página