2

The following code snippet causes an "Uncaught exception warning" when reject(err) is called, although the method is called inside a try/catch statement.

async verifyToken (token: string): Promise<string | object> {
    return new Promise<string | object>( (resolve, reject) => {
        jwt.verify(token, this._publicKey, (err, decoded) => {
             if (err) {
                reject(err);
            } else {
                resolve(decoded);
         };
     });
}

This violates my understanding about promisses and the async/await pattern, because up to now I do not expect an unhandling error if the function is called inside a try/catch statement.

try {
    const obj = await verifyToken(token);
} catch (err) {
    ...
}

At the moment I avoid this problem with a work arround.

async verifyToken (token: string): Promise<string | object> {
    let cause: any;
    const rv = await new Promise<string | object>( (resolve, reject) => {
        jwt.verify(token, this._publicKey, (err, decoded) => {
            if (err) {
                cause = err;
                resolve(undefined);
            } else {
                resolve(decoded);
            };
        })
    });
    if (rv) {
        return rv;
    } else if (cause instanceof Error) {
        throw cause;
    } else {
        throw new Error('invalid token');
    }
}

My questions:

  • Why does the catch not solve this problem?
  • Is there any better solution to avoid an unhandled error in promisses inside an async function?
Manfred Steiner
  • 1,215
  • 2
  • 13
  • 27
  • Your `try`/`catch` code is correct, so something else is going on. Please update your question with a [mcve] demonstrating the problem, ideally a **runnable** one using Stack Snippets (the `[<>]` toolbar button; [here's how to do one](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-do-a-runnable-example-with-stack-snippets-how-do-i-do-tha)). – T.J. Crowder Dec 17 '17 at 12:28
  • 1
    IIRC this is just an IDE problem where the debugger pauses on handled rejections as well, you are not actually getting an `unhandledRejection` event in node right? – Bergi Dec 17 '17 at 12:28
  • 1
    @Bergi, your are right, I am working with vscode and the IDE stops at this line when automatic breakpoints for Uncaught Exceptions are enabled. I am using Node.js v8.9.3 and vscode 1.18.1. – Manfred Steiner Dec 17 '17 at 13:30

2 Answers2

0

I know this is old, and I'm sorry but I just stumbled upon this a moment ago searching for something else, but I think I have the answer to this specific problem.

Promise will neither resolve nor reject in the cast that jwt.verify() throws its own error. Guessing that the lib is jsonwebtoken or something like that I suspect that you have passed an invalid token and that the method does a throw new Error('Invalid token')

I think further to that you're mixing some ideas if you are going to return a new Promise you likely shouldn't use the async keyword.

Suggestion,

function verifyToken(token: string): Promise<ABetterType> {
  return new Promise((resolve, reject) => {
    try {
      jwt.verify(token, this._publicKey, (err, decoded) => {
        if (err) {
          reject(err)
          return;
        }
        resolve(decoded)
      }
    } catch (err) {
      reject(err)
    }
  });
}

OR (what I think I would've tried)

function verifyToken(token: string): Promise<ABetterType> {
  return new Promise((resolve, reject) => {
    try {
      const decoded = jwt.verify(token, this._publicKey);
      resolve(decoded);
    } catch (err) {
      reject(err);
    }
  });
}
alexander.teno
  • 109
  • 1
  • 11
-1

with async/await:

//some async function
try {
    let response = await getAllPosts();
} catch(e) {`enter code here`
    console.log(e);
}

More here: "Uncaught (in promise) undefined" error when using with=location in Facebook Graph API query