From 05a2be2f075a87ffb848ee6da063d13ce6dc3503 Mon Sep 17 00:00:00 2001 From: Dat PHAM HOANG Date: Tue, 8 Nov 2022 18:17:25 +0700 Subject: [PATCH] TF-1008 Create the SetError Handler --- .../base/mixin/handle_error_mixin.dart | 53 +++++++++++++++++++ .../set_method_error_handler_mixin.dart | 7 +++ 2 files changed, 60 insertions(+) create mode 100644 lib/features/base/mixin/handle_error_mixin.dart create mode 100644 model/lib/error_type_handler/set_method_error_handler_mixin.dart diff --git a/lib/features/base/mixin/handle_error_mixin.dart b/lib/features/base/mixin/handle_error_mixin.dart new file mode 100644 index 000000000..13785edfc --- /dev/null +++ b/lib/features/base/mixin/handle_error_mixin.dart @@ -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? notDestroyedHandlers, + Set? notUpdatedHandlers, + Set? 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> _handleInChain(MapEntry setError, Set? handlers) { + try { + handlers!.firstWhere((handler) => handler.call(setError)); + return const None>(); + } catch (e) { + logError('HandleSetErrorMixin::chainHandle(): [Exception] $e'); + return Some>(setError); + } + } + + void _handleRemainedError(SetMethodErrorHandler? unCatchErrorHandler, Option> optionError) { + final remainedError = optionError.toNullable(); + if (remainedError != null) { + logError('HandleSetErrorMixin::_handleRemainedError(): $remainedError'); + unCatchErrorHandler?.call(remainedError); + } + } +} diff --git a/model/lib/error_type_handler/set_method_error_handler_mixin.dart b/model/lib/error_type_handler/set_method_error_handler_mixin.dart new file mode 100644 index 000000000..f8271644d --- /dev/null +++ b/model/lib/error_type_handler/set_method_error_handler_mixin.dart @@ -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; + +/// Returns true if handle [setError] successfully and otherwise +typedef SetMethodErrorHandler = bool Function(MapEntry setError);