-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Err.unwrap()'s behavior and the object it throws #48
Comments
We need TypeScript 4.6+ to be able to construct Error objects with the cause property[1]. The property is useful to chain errors and, on the catching side, to see what error caused the error we just caught. I want to have this in place to implement [3] which satisfies a need we ourselves have[4]. tslib upgraded in lockstep because otherwise we get FAIL test/result.test.ts ● Test suite failed to run test/result.test.ts:121:29 - error TS2807: This syntax requires an imported helper named '__spreadArray' with 3 parameters, which is not compatible with the one in 'tslib'. Consider upgrading your version of 'tslib'. 121 const all4 = Result.all(...([] as Result<string, number>[])); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error [2] microsoft/TypeScript#45167 [3] vultix#34 [4] vultix#48
I want to have this in place to implement [1] which satisfies a need we ourselves have[2]. This is needed because the cause property is only availble in the es2022 TS profile[3]. [1] vultix#34 [2] vultix#48 [3] microsoft/TypeScript#47020
We need TypeScript 4.6+ to be able to construct Error objects with the cause property[1]. The property is useful to chain errors and, on the catching side, to see what error caused the error we just caught. I want to have this in place to implement [3] which satisfies a need we ourselves have[4]. tslib upgraded in lockstep because otherwise we get FAIL test/result.test.ts ● Test suite failed to run test/result.test.ts:121:29 - error TS2807: This syntax requires an imported helper named '__spreadArray' with 3 parameters, which is not compatible with the one in 'tslib'. Consider upgrading your version of 'tslib'. 121 const all4 = Result.all(...([] as Result<string, number>[])); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error [2] microsoft/TypeScript#45167 [3] vultix#34 [4] vultix#48
I want to have this in place to implement [1] which satisfies a need we ourselves have[2]. This is needed because the cause property is only availble in the es2022 TS profile[3]. [1] vultix#34 [2] vultix#48 [3] microsoft/TypeScript#47020
This is useful to get access to the "underlying" error in global exception-handling code for example. Feature requested in [1] and I had a question about something similar as well[2]. [1] vultix#34 [2] vultix#48 This change depends on #17 and #19 to be merged first (then the branch will be updated to contain these changes and the build will succeed).
Hi. I think option 1 is best even though it's a breaking change. |
I vote for the Option 1. In order to maintain backward compatibility I have some ideas;
|
FYI after I read about JS error cause in #34 (comment) I decided to go with option 2 in the |
This (either option 1 or 2) brings enormous value to me. Can I help with this to speed up the release? |
You can try our fork where option 2 is implemented: |
Hi - I am currently attempting this using your fork but I can’t seem to find the relevant info in the docs. Would you be able to point to or provide an example of accessing the original err value? |
Oh yeah, that's a good point. lune-climate#50 should address that (to an extent) – you access it through the [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause |
Hi team, I'm facing exactly the same issue, does we have any eta to update the main project with the fix that are in es? |
By the way, the documentation seems to imply that the desired behavior is the actual behavior: let goodResult = new Ok(1);
let badResult = new Err(new Error('something went wrong'));
goodResult.unwrap(); // 1
badResult.unwrap(); // throws Error("something went wrong") @vultix would you entertain a PR that throws an |
Err.unwrap()
does this right nowwhich kind of makes things inconvenient for my use case. This is the plan I had to gradually introduce
ts-results
into a project – convertto
The point is that the project's error handling is exception-based right now and different actions (HTTP responses for example with different HTTP status codes) are taken based on the exception types and properties.
I assumed
unwrap()
was actually throwing the original error which would make it possible to perform a greadual conversion like above while keeping the exact same behavior. I was wrong and I'm wondering what can be done about it. It's not even possible to modify our code to handle theerrors thrown by
unwrap()
because the errors thrown don't have a reference to the original errors.I see the following options:
Modify
unwrap()
to throw the original value instead of creating a new one.Upsides: no new API introduced
Downsides: we no longer get the
Tried to unwrap Error: ...
message, it would be backward incompatible in a wayModify the error thrown to have a reference to the original error like so
Upsides: no new API introduced
Downsides: handling of the errors is slightly more verbose
Add a new API to throw just the original error value
Upsides:
unwrap()
remains focused on what it does, concerns are separatedDownsides: it's a new API and increases complexity, requires a name (can't say I know what I'd call it)
The text was updated successfully, but these errors were encountered: