You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
throwArgumentError('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.
The text was updated successfully, but these errors were encountered:
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
In
lib/features/user/domain/entities/role.dart
, we should throw something of typeError
instead ofException
.Reasoning
The use of
Exception
is generally for expected exceptions, whereasError
is for unexpected programming errors. In the case of thefromJson
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 ofError
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 anArgumentError
: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.
The text was updated successfully, but these errors were encountered: