It's not possible to distinguish between errors

Moved from GitHub dgraph-js/35

Posted by ondrowan:

Since this library uses only native Error, it’s hard to distinguish the reason why transaction failed. The only way at the moment is to compare error message, which is very cumbersome. I’d like to be able to do the following:

try {
    // some transaction code
} catch (e) {
    if (e instanceof errors.AbortedError) {
         // retry
    }
}

Does this make sense or is there any better way to achieve this?

gpahal commented :

dgraph-js doesn’t use different error types but uses global error constants, so you can do:

try {
    // some transaction code
} catch (e) {
    if (e === dgraph.ERR_ABORTED) {
         // retry
    }
}

Look at file src/errors.ts for all the error values.

ondrowan commented :

I didn’t realize those errors were instantiated only once. Thanks :+1:

felixfbecker commented :

This is not a good practice, because stack traces are generated at the time of instantiating the error. Therefor the error won’t have a useful stacktrace