TF-1008 Create the SetError Handler
This commit is contained in:
committed by
Dat H. Pham
parent
b3945491fb
commit
05a2be2f07
@@ -0,0 +1,53 @@
|
||||
import 'package:core/core.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/error/set_error.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/id.dart';
|
||||
import 'package:model/error_type_handler/set_method_error_handler_mixin.dart';
|
||||
|
||||
mixin HandleSetErrorMixin {
|
||||
void handleSetErrors({
|
||||
SetMethodErrors? notDestroyedError,
|
||||
SetMethodErrors? notUpdatedError,
|
||||
SetMethodErrors? notCreatedError,
|
||||
Set<SetMethodErrorHandler>? notDestroyedHandlers,
|
||||
Set<SetMethodErrorHandler>? notUpdatedHandlers,
|
||||
Set<SetMethodErrorHandler>? notCreatedHandlers,
|
||||
SetMethodErrorHandler? unCatchErrorHandler
|
||||
}) {
|
||||
if (notDestroyedError != null && notDestroyedError.isNotEmpty) {
|
||||
notDestroyedError.entries
|
||||
.map((e) => _handleInChain(e, notDestroyedHandlers))
|
||||
.map((optionError) => _handleRemainedError(unCatchErrorHandler, optionError));
|
||||
}
|
||||
|
||||
if (notCreatedError != null && notCreatedError.isNotEmpty) {
|
||||
notCreatedError.entries
|
||||
.map((e) => _handleInChain(e, notCreatedHandlers))
|
||||
.map((optionError) => _handleRemainedError(unCatchErrorHandler, optionError));
|
||||
}
|
||||
|
||||
if (notUpdatedError != null && notUpdatedError.isNotEmpty) {
|
||||
notUpdatedError.entries
|
||||
.map((e) => _handleInChain(e, notUpdatedHandlers))
|
||||
.map((optionError) => _handleRemainedError(unCatchErrorHandler, optionError));
|
||||
}
|
||||
}
|
||||
|
||||
Option<MapEntry<Id, SetError>> _handleInChain(MapEntry<Id, SetError> setError, Set<SetMethodErrorHandler>? handlers) {
|
||||
try {
|
||||
handlers!.firstWhere((handler) => handler.call(setError));
|
||||
return const None<MapEntry<Id, SetError>>();
|
||||
} catch (e) {
|
||||
logError('HandleSetErrorMixin::chainHandle(): [Exception] $e');
|
||||
return Some<MapEntry<Id, SetError>>(setError);
|
||||
}
|
||||
}
|
||||
|
||||
void _handleRemainedError(SetMethodErrorHandler? unCatchErrorHandler, Option<MapEntry<Id, SetError>> optionError) {
|
||||
final remainedError = optionError.toNullable();
|
||||
if (remainedError != null) {
|
||||
logError('HandleSetErrorMixin::_handleRemainedError(): $remainedError');
|
||||
unCatchErrorHandler?.call(remainedError);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import 'package:jmap_dart_client/jmap/core/error/set_error.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/id.dart';
|
||||
|
||||
typedef SetMethodErrors = Map<Id, SetError>;
|
||||
|
||||
/// Returns true if handle [setError] successfully and otherwise
|
||||
typedef SetMethodErrorHandler = bool Function(MapEntry<Id, SetError> setError);
|
||||
Reference in New Issue
Block a user