最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to catch all errors that are thrown asyncronously in a Promise - Stack Overflow

programmeradmin1浏览0评论

The problem I am facing is that somewhere in the code I am working with there is an error that I cannot catch. The simplest example of this would be the code below:

try {
    new Promise((res, rej) => rej(new Error('You cannot catch me. Haha')));
} catch (err) {
    conosle.error(err);
}

I don't want to alter the code much. I want to be able to catch and ignore some errors like that. I am trying to replace Promise by wrapping it so that i can catch all errors of that sort.

I have tried asking ChatGPT and it doesn't help much, so Googling will most probably not help also.

EDIT:

The error I am getting is a native DOMException, and the suggestions in the docs don't help. I want to catch and ignore the error. I think I will just go with Keith's suggestion under my answer.

What I want to do is, to catch all errors in every promise there is, so that I can decide if I should rethrow it again or just ignore it.

The problem I am facing is that somewhere in the code I am working with there is an error that I cannot catch. The simplest example of this would be the code below:

try {
    new Promise((res, rej) => rej(new Error('You cannot catch me. Haha')));
} catch (err) {
    conosle.error(err);
}

I don't want to alter the code much. I want to be able to catch and ignore some errors like that. I am trying to replace Promise by wrapping it so that i can catch all errors of that sort.

I have tried asking ChatGPT and it doesn't help much, so Googling will most probably not help also.

EDIT:

The error I am getting is a native DOMException, and the suggestions in the docs don't help. I want to catch and ignore the error. I think I will just go with Keith's suggestion under my answer.

What I want to do is, to catch all errors in every promise there is, so that I can decide if I should rethrow it again or just ignore it.

Share Improve this question edited Jan 19 at 18:50 ibodi asked Jan 17 at 16:22 ibodiibodi 1,7764 gold badges25 silver badges43 bronze badges 9
  • 1 try { await new Promise(........ – Keith Commented Jan 17 at 16:30
  • @Keith As I said, I don't want to alter the code much. I want to catch these errors, without modifying stuff much. – ibodi Commented Jan 17 at 16:31
  • 2 "without modifying stuff much": well in your self-answer it seems nothing remained unchanged. I believe Keith's suggested changes are tiny compared what you have changed yourself in your answer. Moreover, your answer has code that is not in the question. Weird. – trincot Commented Jan 17 at 21:23
  • somewhere in the code I am working with there is an error that I cannot catch - if it's your code, fix it; if it's a library, make sure you're using it right, otherwise report an issue with the libraries author - or at least, that's what I would do – Bravo Commented Jan 17 at 22:19
  • 3 Keith added a single await, which is 'too much' and you added 40 lines of code that modify built-ins, which is acceptable? You should have been more clear in your question – Evert Commented Jan 18 at 1:37
 |  Show 4 more comments

2 Answers 2

Reset to default -1

Promises have their own catch clause. You can catch it through that.

try {
  new Promise((res, rej) => rej(new Error('You cannot catch me. Haha')))
             .catch(error => console.log("caught"));
} catch (err) {
    conosle.error(err);
}

After some fighting and effort I came up with this solution:

const P = window.Promise;

const errorIgnorer = (err) => {
    if (err?.message?.includes('The play() request was interrupted by a call to pause()')
        || err?.message?.includes('Already in offline state')
        || err?.message?.includes('Pending connection was cancelled due to a disconnect request')) {
        return;
    }
    throw err;
};

function PromiseTemp(cb) {
    let promise = new P(cb);

    promise = promise.catch(errorIgnorer);

    function then(onFulfilled, onRejected) {
        const temp = P
            .prototype
            .then
            .call(
                this,
                onFulfilled,
                onRejected
            )
            .catch(errorIgnorer);

        temp.then = then;
        // eslint-disable-next-line @typescript-eslint/no-use-before-define
        temp.catch = catchFn;

        return temp;
    }

    function catchFn(onRejected) {
        const temp = then.call(this, null, onRejected);
        temp.then = then;
        temp.catch = catchFn;
        return temp;
    }

    promise.then = then;
    promise.catch = catchFn;

    return promise;
}

Object.setPrototypeOf(PromiseTemp.prototype, P.prototype);
Object.setPrototypeOf(PromiseTemp, P);

window['Promise'] = PromiseTemp as any;

This solution catches all errors generated inside of promises and ignores errors that errorIgnorer function "filters".

发布评论

评论列表(0)

  1. 暂无评论