tryCatchIntoResultWithEnsureError
- This function converts the returend value from producer into
Ok(T)
. - If producer throw a value that is an instance of
Error
of current realm, this returns it with wrappingErr(Error)
. - Otherwise, if producer throw a value that is not an instance of current realm including
a one created from cross-ream
Error
constructor (e.g.node:vm
, iframe), then this operator creates a newError
object created from the current realm's constructor and the thrown value to itsError.cause
, make itErr(Error)
, and return it finally.
This just returns it directly if the thrown value is an Error
instance of the current realm.
We can do this due to that we can regard it is happen in the same operation step to throw a value
unlike the combination of tryCatchIntoResult
+ mapErrForResult
.
This shortcut can reduce an unnecessary Error.cause
chain.
NOTE:
- An user should narrow the scope of producer to make it predictable that is in
Err(E)
. - This function requires ES2022's
Error.cause
to get an actual thrown object. - Basically, we don't recomment to use this to create a
Result<T, E>
. Generally, you should define anErr(E)
by depending on an use case context Use this operator just to make a bridge to existing codebase that you cannot inspect deeply to details.