erwinleonardy
erwinleonardy

Reputation: 486

Unhandled Error returned in Promise Rejection (Typescript)

So, I am not really new when it comes to USING Promises. I understand and have used .then(), .catch(), Promise.all() before. I used them chiefly to fetch API calls.

However, I am a complete beginner when it comes to CREATING my own promises.

So, I was attempting to create my own promise and the reject() function kept on giving me the unhandled error (my rejection message) message. Although, the resolve() function worked as expected.

Here is my (simplified) codes:

const request = require('request-promise');

return new Promise(async (resolve, reject) => {
    try {
        /*
         * Some other codes that might throw other exceptions
         */
        ....
        ....

        const options = "ASSUME THIS IS A CALL TO A THIRD-PARTY API";

        await request(options)
            .then((val: any) => {
                ...
                // I had no issue with this. Resolve returns properly
                resolve(val);                     
            })
            .catch((e: any) => {
                ...

                // I would need to translate the error message from this 
                // Third-Party API. But, this reject function keeps on 
                // returning me an "unhandled error" issue
                reject(translateMsg(e.message, "fr"));
            });
    }
    catch(err) {
        reject("Oops other errors detected");
    }
}

Note: This error message was shown in my Firebase Function's log. I am not sure if the issue lies in my codes or somewhere else.

Thank you!

(EDIT 1) I did some other processing (translation, etc.) in the request.catch() statement. So, I need to return a new Error message and not the default e object.

(EDIT 2) Assume that translateMsg() is well-tested and it ALWAYS returns a string.

Upvotes: 1

Views: 1713

Answers (2)

erwinleonardy
erwinleonardy

Reputation: 486

Firstly, thanks guys for pointing out all of the best practices when it comes to handling promises. I am going to fix my codes accordingly.

My issue turned out to be originated from the Google Firebase Function I was using.

Upon revisiting the documentation, I realised that the proper way to handle an error in Firebase Function is by throwing throw new functions.https.HttpsError("") and not Promise.reject().

Thanks again!

Firebase Function Error Handling

Source: https://firebase.google.com/docs/functions/callable

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317828

If your API already returns a promise, there is absolutely no reason to create one of your own. Just use the one you have. Also, mixing async/await with then/catch is rarely advisable.

Honestly, the best course of action is not to add anything at all. Just return request(options), and the caller will receive and promise that already resolves or rejects on its own.

If you absolutely must change what the call receives in terms of resolution or rejection, what you're doing can be simplified down to:

const options = "ASSUME THIS TO BE AN API CALL";
try {
    const val = await request(options)
    // maybe you want to change val here?  Return what you want.
    return val
}
catch (err) {
    throw "Oops other errors detected"
}

Upvotes: 4

Related Questions