简体   繁体   English

Promise Rejection (Typescript) 中返回未处理的错误

[英]Unhandled Error returned in Promise Rejection (Typescript)

So, I am not really new when it comes to USING Promises.所以,在使用 Promises 方面,我并不是什么新鲜事。 I understand and have used .then(), .catch(), Promise.all() before.我了解并使用.then(), .catch(), Promise.all() I used them chiefly to fetch API calls.我主要使用它们来获取 API 调用。

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.所以,我试图创建我自己的 promise 并且reject() function 不断给我unhandled error (my rejection message)消息。 Although, the resolve() function worked as expected.虽然, resolve() function 按预期工作。

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.注意:此错误消息显示在我的 Firebase 函数日志中。 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. (编辑 1)我在request.catch()语句中做了一些其他处理(翻译等)。 So, I need to return a new Error message and not the default e object.因此,我需要返回一条新的错误消息,而不是默认的e object。

(EDIT 2) Assume that translateMsg() is well-tested and it ALWAYS returns a string. (编辑 2)假设translateMsg()已经过良好测试并且它总是返回一个字符串。

If your API already returns a promise, there is absolutely no reason to create one of your own.如果您的 API 已经返回 promise,那么绝对没有理由创建您自己的 API。 Just use the one you have.只需使用您拥有的那个。 Also, mixing async/await with then/catch is rarely advisable.此外,很少建议将 async/await 与 then/catch 混合使用。

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.只需return request(options) ,调用者将收到已经自行解决或拒绝的 promise 。

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"
}

Firstly, thanks guys for pointing out all of the best practices when it comes to handling promises.首先,感谢大家在处理 Promise 时指出所有最佳实践。 I am going to fix my codes accordingly.我将相应地修复我的代码。

My issue turned out to be originated from the Google Firebase Function I was using.我的问题原来源于我使用的Google Firebase Function

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() . 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 函数错误处理

Source: https://firebase.google.com/docs/functions/callable资料来源: https://firebase.google.com/docs/functions/callable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM