Last active
October 13, 2022 15:45
-
-
Save yarn-rp/caa02e0f2ee02d4ca17aac5e5b87baea to your computer and use it in GitHub Desktop.
Cubit handling exceptions vs Cubit handling results
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Exceptions example | |
class AppVersioningCubit extends Cubit<AppVersioningState> { | |
AppVersioningCubit(this.appVersioningService) | |
: super(const AppVersioningState.initial()); | |
final AppVersioningServiceContract appVersioningService; | |
/// Calls appVersioningService.getAppVersion to fetch current app version. | |
/// Then, pass appVersion to validateAppVersionMethod. | |
Future<void> getAndValidateAppVersion() async { | |
emit( | |
const AppVersioningState.loading(), | |
); | |
try { | |
final appVersion = await appVersioningService.getAppVersion(); | |
final appVersionValidationResult = | |
await appVersioningService.validateAppVersion(appVersion); | |
emit( | |
appVersionValidationResult.map( | |
forceUpgrade: (result) => AppVersioningState.forceUpgrade( | |
currentVersion: appVersion, | |
upgradeInformation: result.upgradeInformation, | |
), | |
availableUpgrade: (result) => AppVersioningState.availableUpgrade( | |
currentVersion: appVersion, | |
upgradeInformation: result.upgradeInformation, | |
), | |
// Esta no existiria ya que se trataria como una exception | |
// unexpectedError: (result) => | |
// AppVersioningState.errorValidatingAppVersion( | |
// currentVersion: appVersion, | |
// ), | |
), | |
); | |
} on AppVersioningServiceException catch (exception) { | |
exception.when( | |
getAppVersionError: (exception, stackTrace) => | |
emitErrorGettingAppVersion(exception), | |
validateAppVersionError: () => emitErrorValidatingAppVersion( | |
exception.appVersion, | |
exception, | |
), | |
); | |
} | |
} | |
@visibleForTesting | |
void emitErrorGettingAppVersion(Object? exception) => emit( | |
AppVersioningState.errorGettingAppVersion( | |
exception: exception, | |
), | |
); | |
@visibleForTesting | |
void emitErrorValidatingAppVersion( | |
AppVersion appVersion, | |
Object? exception, | |
) => | |
emit( | |
AppVersioningState.errorValidatingAppVersion( | |
currentVersion: appVersion, | |
exception: exception, | |
), | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Results example | |
class AppVersioningCubit extends Cubit<AppVersioningState> { | |
AppVersioningCubit(this.appVersioningService) | |
: super(const AppVersioningState.initial()); | |
final AppVersioningServiceContract appVersioningService; | |
/// Calls appVersioningService.getAppVersion to fetch current app version | |
/// | |
/// Depending on the result, decides to validate in case of success or emits | |
/// an error state in case of failure. | |
Future<void> getAndValidateAppVersion() async { | |
emit( | |
const AppVersioningState.loading(), | |
); | |
// Fetches app Validation | |
final appVersionResult = await appVersioningService.getAppVersion(); | |
return appVersionResult.map<FutureOr<void>>( | |
success: (success) => validateAppVersion(success.appVersion), | |
failure: (failure) => emitErrorGettingAppVersion(failure.exception), | |
); | |
} | |
/// Validates appVersion previously obtained and emits a valid state for it | |
@visibleForTesting | |
Future<void> validateAppVersion(AppVersion appVersion) async { | |
final appVersionValidationResult = | |
await appVersioningService.validateAppVersion(appVersion); | |
emit( | |
appVersionValidationResult.map( | |
forceUpgrade: (result) => AppVersioningState.forceUpgrade( | |
currentVersion: appVersion, | |
upgradeInformation: result.upgradeInformation, | |
), | |
availableUpgrade: (result) => AppVersioningState.availableUpgrade( | |
currentVersion: appVersion, | |
upgradeInformation: result.upgradeInformation, | |
), | |
unmaintained: (_) => AppVersioningState.unmaintainedApp( | |
currentVersion: appVersion, | |
), | |
usingLastVersion: (_) => AppVersioningState.noUpdatesAvailable( | |
currentVersion: appVersion, | |
), | |
unexpectedError: (result) => | |
AppVersioningState.errorValidatingAppVersion( | |
currentVersion: appVersion, | |
), | |
), | |
); | |
} | |
@visibleForTesting | |
void emitErrorGettingAppVersion(Object? exception) => emit( | |
AppVersioningState.errorGettingAppVersion( | |
exception: exception, | |
), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment