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

Throw ArgumentError instead of Exception in switch-based methods #427

Closed
marfavi opened this issue Apr 23, 2023 · 0 comments · Fixed by #444
Closed

Throw ArgumentError instead of Exception in switch-based methods #427

marfavi opened this issue Apr 23, 2023 · 0 comments · Fixed by #444
Assignees
Labels
code/dev DX; technical debt; issue concerns code readability or quality good first issue

Comments

@marfavi
Copy link
Member

marfavi commented Apr 23, 2023

In lib/features/user/domain/entities/role.dart, we should throw something of type Error instead of Exception.

Reasoning

The use of Exception is generally for expected exceptions, whereas Error is for unexpected programming errors. In the case of the fromJson function, encountering an unknown role should be considered an unexpected programming error, as it indicates that the role mapping is not correctly set up.

Using ArgumentError would be more appropriate in this context, as it is a subclass of Error and is used to indicate that an invalid argument has been passed to a function. In this case, the invalid argument is the unknown role. This change will help maintain consistency in the codebase by using the correct error types for different situations.

Proposed changes

Replace the current Exception with an ArgumentError:

throw ArgumentError('Unknown role $role');

By making this change, we will be better able to identify and handle unexpected programming errors in our application.

Other work

Ensure that similar methods (that essentially maps an enum value to another value) follow this same pattern.

MatchCaseIncompleteException is a class we use for similar purposes; this should either be completely replaced by an ArgumentError or extend some Error type instead of Exception.

@marfavi marfavi added good first issue code/dev DX; technical debt; issue concerns code readability or quality labels Apr 23, 2023
@marfavi marfavi changed the title Return ArgumentError instead of Exception in switch-based methods Throw ArgumentError instead of Exception in switch-based methods Apr 24, 2023
@fremartini fremartini self-assigned this May 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code/dev DX; technical debt; issue concerns code readability or quality good first issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants