everweij/typescript-result

.getOrThrow() to throw the original error?

Closed this issue · 2 comments

Hi! If I run the following code:

    return Result.try(async () => {
      const user = await this.userService.createUser({ email, password, type }).getOrThrow()
      const session = await this.sessionService.createSession(user.id.value, {})
      return { user, session }
    })

The original cause of await this.userService.createUser({ email, password, type }) error is wrapped by a new Error instance by getOrThrow(). Is it really necessary? Can't we just throw the original error?

It makes it a bit cumbersome to check the root cause (i.e. you gotta look at error.cause instead of error)

Hey @pjeziorowski, I understand your point of view. The reason I added the additional wrapping is to make it clearer where an error is coming from in case you use getOrThrow. Like you mentioned the downside is that it can become a bit cumbersome the use error.cause all the time, etc.

So I agree, let's get rid of the additional wrapping. Happy to accept a PR, and otherwise I will check if a have time later this week.

Fixed in v3.0.0