deno_graph
deno_graph copied to clipboard
Implement `std::Error::source()` for errors
ModuleGraphError
and ResolutionError
did not implement Error::source()
, so if a user-provided Resolver
or Loader
returns an error, its cause chain is lost.
This PR implements source()
, but it keeps the existing implementation of Display
. Unfortunately, this means that when anyhow prints the cause chain of errors, it might print some error messages multiple times. For example, when we print a ModuleGraphError::LoadingErr(err)
, then anyhow
first prints the ModuleGraphError
, which displays err
. anyhow
then prints the source of the ModuleGraphError
, which is err
, so err
is displayed again!
There are two possible solutions to this problem:
- Change implementation of
Display
, so thatModuleGraphError::LoadingErr(err)
displays a fixed error message ("The module could not be loaded"
). The disadvantage is that if the caller does not also print the cause chain, the user will not see the actual error! - Change implementation of
Error::source()
, so thatModuleGraphError::LoadingErr(err)
returnserr.source()
instead oferr
. This means thaterr
will not be displayed twice, buterr
will no longer be present in the cause chain.