1

I'm using axios response interceptor and after I cancel a request, I need to break a promise chain. I don't want to add an error check of canceled request for all requests in my application. I've tried bluebird, but it seems it's just promise cancellation, not chain breaking. I have to process errors in the first catch. This diagram shows the problem in general. Latest then and catch are in different files.

Promise
.then((response) => {

)
.catch((error) => {
  // break promise here
})
.then((response) => {
 // skip
 // I don't want any extra checks here!
)
.catch((error) => {
  // skip
  // I don't want any extra checks here!
})
Nald Dev
  • 557
  • 5
  • 15
Nikronis
  • 141
  • 2
  • 11
  • @Liam I know, that's why I'm trying to find any library that can do it. I need to overcome this problem by all means, because I'll have to add to the latest catch (in many files) the same error handling. – Nikronis Oct 28 '19 at 16:35
  • 1
    @Liam - Your statement about only having one `.catch()` on a promise chain is absolutely not true. You can have as many as you want. And, inside a `.catch()` you can rethrow and the code will then hit the next `.catch()` in the chain. Or, you can return a value and it will hit the next `.then()` in the chain which could reject causing it to hit the next `.catch()` after that. You can do this as many times as you want in the promise chain. – jfriend00 Oct 28 '19 at 17:22
  • [SO Promise question](https://stackoverflow.com/q/31324110/463206). Interesting, detailed discussion of then/resolve/reject. Also dive into links provided throughout. It seems to me that any given `.then` can react to the state of its invoking `.then` and react accordingly - i.e. `return` immediately if necessary; note `.then()` takes 2 parameters. Eventually the "main" Promise invokes its "resolver" which, if supplied by you as the Promise constructor parameter, does whatever based on the state as set by the `.then` chain. – radarbob Oct 28 '19 at 17:44
  • 1
    Did you have a look at [How to properly break out of a promise chain](https://stackoverflow.com/q/29499582/1048572)? – Bergi Oct 28 '19 at 19:47

2 Answers2

1

Another option is to throw a custom error that can be caught in a singular catch block at the very end like so:

const errorHandler = require('path/to/handler')
class ProcessingError1 extends Error {
    constructor(message) {
        super(message);
        this.name = "ProcessingError1";
    }
}
class ProcessingError2 extends Error {
    constructor(message) {
        this.message = message;
        this.name = "ProcessingError2";
    }
}
const process1 = async () => {
    throw new ProcessingError1("Somethign went wrong");
};
const process2 = async () => {
    return { some: "process" };
};
const someApiCall = async () => ({ some: "response" });

someApiCall()
    .then(process1)
    .then(process2) // process2 is not run if process1 throws an error
    .catch(errorHandler);
// ErrorHandler.js

module.exports = e => {
        if (e instanceof ProcessingError1) {
            // handle error thrown from process1
        }
        else if (e instanceof ProcessingError2) {
            // handle error thrown from process2
        }
        else {
            // handle other errors as needed..
        }
    }
dillon
  • 721
  • 6
  • 16
  • The problem is that I'll have to add this error handling in dozens of files and the code will be the same. That's what I'm trying to avoid. – Nikronis Oct 29 '19 at 09:13
  • Just have the code for the catch imported from another file? – dillon Oct 30 '19 at 12:26
  • I've just updated my example to show how it would look to import your error handling to prevent the duplication – dillon Oct 30 '19 at 12:31
  • All right. It seems like the only option in the current situation. Thanks – Nikronis Oct 30 '19 at 17:56
0

just keep only one catch block at the end to collect all the errors triggered by your promise chain.

if one promise throws the following in the chain are not executed

Promise
.then((response) => {

)
.then((response) => {
 // skip
 // I don't want any extra checks here!
)
.catch((error) => {
  // skip
  // I don't want any extra checks here!
})
Karim
  • 8,454
  • 3
  • 25
  • 33