AnalogIO / coffeecard_app

Cross-platform coffee card app for Cafe Analog
https://www.cafeanalog.dk/app
MIT License
6 stars 1 forks source link

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

Closed marfavi closed 1 year ago

marfavi commented 1 year ago

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.