Skip to content

refactor: make RuntimeError class reusable across packages#44398

Closed
AndrewKushnir wants to merge 3 commits intoangular:masterfrom
AndrewKushnir:reuse-runtime-error
Closed

refactor: make RuntimeError class reusable across packages#44398
AndrewKushnir wants to merge 3 commits intoangular:masterfrom
AndrewKushnir:reuse-runtime-error

Conversation

@AndrewKushnir
Copy link
Contributor

This PR contains a few commits that make the RuntimeError class reusable across packages + performs some refactoring to use the class in the Forms package.

Note: this is a replacement PR for #44348.

PR Type

What kind of change does this PR introduce?

  • Refactoring (no functional changes, no api changes)

Does this PR introduce a breaking change?

  • Yes
  • No

@AndrewKushnir AndrewKushnir added state: WIP refactoring Issue that involves refactoring or code-cleanup area: core Issues related to the framework runtime area: forms target: patch This PR is targeted for the next patch release labels Dec 8, 2021
@ngbot ngbot bot modified the milestone: Backlog Dec 8, 2021
@AndrewKushnir AndrewKushnir added action: review The PR is still awaiting reviews from at least one requested reviewer and removed state: WIP labels Dec 8, 2021
@AndrewKushnir AndrewKushnir marked this pull request as ready for review December 8, 2021 05:12
@pullapprove pullapprove bot requested a review from jelbourn December 8, 2021 05:12
@AndrewKushnir
Copy link
Contributor Author

Hi @twerske, this PR updates the RuntimeError class (and related logic) to make it reusable across Angular packages. As a part of this change, I'm starting to use a reserved bit of the error code to differentiate Angular packages. The error code prefix used to be hardcoded as NG0 (see here) and now the digit that follows NG would be different per package. So the format for the error codes in the core package remains NG0xxx, when for forms it'd start with NG1xxx and NG2xxx for common (in the future we can use other digits for Router, etc).

Just wanted to check with you if this use of the error code bit is aligned with the idea that was implemented initially.

Thank you.

@AndrewKushnir
Copy link
Contributor Author

@alxhub @dylhunn @jessicajaniuk thanks for your review in the previous version of this PR (#44348). I've created a new one with a slightly different approach. Instead of creating a new class for each package (as it was in #44348), this PR reuses the same class and each package contains a thin wrapper that supplies the package id. The motivation for this is to maintain easy error handling (try { ... } catch(e) { if (e instanceof RuntimeError) { ... } }) which wouldn't be possible if we create new classes per package.

Could you please review this PR when you get a chance?

Copy link
Contributor

@jessicajaniuk jessicajaniuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🍪

reviewed-for: size-tracking, fw-core, fw-common

Copy link
Contributor

@dylhunn dylhunn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewed-for: size-tracking, fw-core, fw-common, fw-forms

@twerske
Copy link
Contributor

twerske commented Dec 8, 2021

Thanks for tagging me @AndrewKushnir!

One design concern is that NG1 through NG9 are already taken by compile time errors so I'm not sure this can be designated and reassigned out per package as this PR introduces. You can see some of these error documented here: https://angular.io/errors/

For example NG1001: Decorator argument is not an object literal does not fit this new classification of forms

In the initial design for runtime errors (internal design can be found here) one of the constraints was that NG1-9 are already reserved for compiler errors so NG0 was proposed for all runtime errors and then the second digit indicates category.

Is there a way to add or use the categories in the second digit per package? Love the addition of additional error codes in a more standard format and if there's space to add docs as well that'd be AMAZING!

@AndrewKushnir AndrewKushnir force-pushed the reuse-runtime-error branch 2 times, most recently from bd7acbe to 0aa574a Compare December 9, 2021 00:56
@AndrewKushnir
Copy link
Contributor Author

AndrewKushnir commented Dec 9, 2021

Thanks for the review everyone!

@twerske thanks for your feedback, that makes sense. I've reworked the changes a bit to retain the NG0 prefix and annotated errors.ts file in forms and common packages to mention which error code range should be used:

  • 100-999 - is the range for core
  • 1000-1999 - the range for forms
  • 2000-2999 - for common
  • ... similarly, we'll allocate ranges for other packages as well

The benefit is that we'll use the bit next to NG0 as a namespace (i.e. NG01xxx would be for forms, NG02xxx for common, etc). One downside is that the error code length for non-core errors would be 1 character longer.

This commit updates the code around the `RuntimeError` class to make it more reusable between packages (currently it's only usable inside the `core` package). Specifically:
- the error formatting logic was updated to handle cases when there is no error message provided
- there is no special Set that contains a set of error codes for which we have guides on angular.io. Instead, this is now encoded into the error code itself (making such codes negative integers). Having a separate Set makes it non-tree-shakable, which we want to avoid.

This change should allow to employ the `RuntimeError` class in other packages to further standardize this subsystem and make the errors thrown by the framework consistent.

As a part of the refactoring, the `common` package code was also updated to follow the same logic as `core`, since the `RuntimeError` class was used there as well.
This commit moves some logic to make the location of runtime error codes consistent across packages. Now all error codes are located in `packages/core/src/errors.ts` file.
@AndrewKushnir AndrewKushnir added action: merge The PR is ready for merge by the caretaker and removed action: presubmit The PR is in need of a google3 presubmit labels Jan 6, 2022
@AndrewKushnir
Copy link
Contributor Author

Note: like the compiler errors, we should guard the runtime error enums with a public-api test.

Agreed, I will followup with a new PR once this one lands.

@AndrewKushnir AndrewKushnir added the merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note label Jan 6, 2022
@AndrewKushnir
Copy link
Contributor Author

Note to the Caretaker: this PR would require a local mod update in g3, please ping me for additional info.

@atscott atscott added target: minor This PR is targeted for the next minor release and removed target: patch This PR is targeted for the next patch release labels Jan 6, 2022
@atscott
Copy link
Contributor

atscott commented Jan 6, 2022

This PR was merged into the repository by commit 67df935.

@atscott atscott closed this in 66e59bc Jan 6, 2022
atscott pushed a commit that referenced this pull request Jan 6, 2022
This commit moves some logic to make the location of runtime error codes consistent across packages. Now all error codes are located in `packages/core/src/errors.ts` file.

PR Close #44398
atscott pushed a commit that referenced this pull request Jan 6, 2022
This commit performs some refactoring of the AbstractControl-based classes to employ shared `RuntimeError` class and also updates the code to avoid duplication and improve minification.

PR Close #44398
AndrewKushnir added a commit to AndrewKushnir/angular that referenced this pull request Jan 7, 2022
…4398)

This commit updates the code around the `RuntimeError` class to make it more reusable between packages (currently it's only usable inside the `core` package). Specifically:
- the error formatting logic was updated to handle cases when there is no error message provided
- there is no special Set that contains a set of error codes for which we have guides on angular.io. Instead, this is now encoded into the error code itself (making such codes negative integers). Having a separate Set makes it non-tree-shakable, which we want to avoid.

This change should allow to employ the `RuntimeError` class in other packages to further standardize this subsystem and make the errors thrown by the framework consistent.

As a part of the refactoring, the `common` package code was also updated to follow the same logic as `core`, since the `RuntimeError` class was used there as well.

PR Close angular#44398
AndrewKushnir added a commit to AndrewKushnir/angular that referenced this pull request Jan 7, 2022
This commit moves some logic to make the location of runtime error codes consistent across packages. Now all error codes are located in `packages/core/src/errors.ts` file.

PR Close angular#44398
AndrewKushnir added a commit to AndrewKushnir/angular that referenced this pull request Jan 7, 2022
This commit performs some refactoring of the AbstractControl-based classes to employ shared `RuntimeError` class and also updates the code to avoid duplication and improve minification.

PR Close angular#44398
atscott pushed a commit that referenced this pull request Jan 7, 2022
…44652)

This commit updates the code around the `RuntimeError` class to make it more reusable between packages (currently it's only usable inside the `core` package). Specifically:
- the error formatting logic was updated to handle cases when there is no error message provided
- there is no special Set that contains a set of error codes for which we have guides on angular.io. Instead, this is now encoded into the error code itself (making such codes negative integers). Having a separate Set makes it non-tree-shakable, which we want to avoid.

This change should allow to employ the `RuntimeError` class in other packages to further standardize this subsystem and make the errors thrown by the framework consistent.

As a part of the refactoring, the `common` package code was also updated to follow the same logic as `core`, since the `RuntimeError` class was used there as well.

PR Close #44398

PR Close #44652
atscott pushed a commit that referenced this pull request Jan 7, 2022
This commit moves some logic to make the location of runtime error codes consistent across packages. Now all error codes are located in `packages/core/src/errors.ts` file.

PR Close #44398

PR Close #44652
atscott pushed a commit that referenced this pull request Jan 7, 2022
This commit performs some refactoring of the AbstractControl-based classes to employ shared `RuntimeError` class and also updates the code to avoid duplication and improve minification.

PR Close #44398

PR Close #44652
AndrewKushnir added a commit to AndrewKushnir/angular that referenced this pull request Jan 10, 2022
Runtime error codes in the Core, Common and Forms packages were not included into the `public-api` group reviews. This commit creates the necessary golden files to keep track of further changes in the runtime codes.

This is a followup from angular#44398 (comment).
atscott pushed a commit that referenced this pull request Jan 12, 2022
Runtime error codes in the Core, Common and Forms packages were not included into the `public-api` group reviews. This commit creates the necessary golden files to keep track of further changes in the runtime codes.

This is a followup from #44398 (comment).

PR Close #44677
atscott pushed a commit that referenced this pull request Jan 12, 2022
Runtime error codes in the Core, Common and Forms packages were not included into the `public-api` group reviews. This commit creates the necessary golden files to keep track of further changes in the runtime codes.

This is a followup from #44398 (comment).

PR Close #44677
amitbeck pushed a commit to amitbeck/angular that referenced this pull request Jan 13, 2022
Runtime error codes in the Core, Common and Forms packages were not included into the `public-api` group reviews. This commit creates the necessary golden files to keep track of further changes in the runtime codes.

This is a followup from angular#44398 (comment).

PR Close angular#44677
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Feb 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: core Issues related to the framework runtime area: forms merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note refactoring Issue that involves refactoring or code-cleanup target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants