Skip to content
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

refactor: Don't restrict error type for Result<T, E> #23265

Merged
merged 5 commits into from
Jul 10, 2023

Conversation

zharinov
Copy link
Collaborator

@zharinov zharinov commented Jul 9, 2023

Changes

Error can not be right decision for representing error results: for example, we not necessarily want to have stacktraces.

There is no reason why error can't be of any type, including something lightweight like just true literal.

This PR refactors error type and also provides some improvements for the Result API.

Names has been aligned to zod schema library, as it solves very similar problems for us.

Context

Documentation (please check one with an [x])

  • I have updated the documentation, or
  • No documentation update is required

How I've tested my work (please select one)

I have verified these changes via:

  • Code inspection only, or
  • Newly added/modified unit tests, or
  • No unit tests but ran on a real repository, or
  • Both unit tests + ran on a real repository

@zharinov zharinov requested review from viceice and rarkins July 9, 2023 12:44
@rarkins
Copy link
Collaborator

rarkins commented Jul 9, 2023

Could we not wrap anything in our own Error type?

@zharinov
Copy link
Collaborator Author

zharinov commented Jul 9, 2023

Could we not wrap anything in our own Error type?

Please, elaborate on that, I'm not sure I understand what you mean

@rarkins
Copy link
Collaborator

rarkins commented Jul 9, 2023

What's wrong with using Error for everything if we can extend it however we like?

@zharinov
Copy link
Collaborator Author

zharinov commented Jul 9, 2023

Here is the Rubygems-related code I'm refactoring at the moment:

export type VersionsResult = Result<
  string[],
  'unsupported-api' | 'package-not-found'
>;

I think for cases like this, Error class is too much:

  • we have to declare couple classes separately
  • in cases when Result is used just for control flow, we don't want stacktraces to be constructed, especially inside loops.

But when we need it, we still can have it I guess.

@zharinov
Copy link
Collaborator Author

zharinov commented Jul 9, 2023

Actually, maybe the single hierarchy of errors is the good thing, I just don't want them to be descendants of Error class. Hierarchy of nested union types (Error class included) looks good to me, though.

@rarkins rarkins added this pull request to the merge queue Jul 10, 2023
Merged via the queue into renovatebot:main with commit 8f39f50 Jul 10, 2023
36 checks passed
@rarkins rarkins deleted the refactor/result-error-any-type branch July 10, 2023 09:53
@renovate-release
Copy link
Collaborator

🎉 This PR is included in version 36.7.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants