From a8e826f58b33ed66d7eb668dde6bf51db88a6c48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malo=20L=C3=A9on?= Date: Fri, 18 Aug 2023 18:20:52 +0200 Subject: [PATCH] fix: fix unimplemented error in listenable use case --- .../exceptions/notification_exeption.dart | 7 +++- .../core/helpers/app_exception_helper.dart | 26 ++++++++++++++ .../cloud_messaging_repository_impl.dart | 34 +++++-------------- .../listen_notification_use_case.dart | 4 ++- 4 files changed, 44 insertions(+), 27 deletions(-) create mode 100644 packages/wyatt_cloud_messaging_bloc_base/lib/src/core/helpers/app_exception_helper.dart diff --git a/packages/wyatt_cloud_messaging_bloc_base/lib/src/core/exceptions/notification_exeption.dart b/packages/wyatt_cloud_messaging_bloc_base/lib/src/core/exceptions/notification_exeption.dart index 132ef277..048c6476 100644 --- a/packages/wyatt_cloud_messaging_bloc_base/lib/src/core/exceptions/notification_exeption.dart +++ b/packages/wyatt_cloud_messaging_bloc_base/lib/src/core/exceptions/notification_exeption.dart @@ -17,5 +17,10 @@ import 'package:wyatt_architecture/wyatt_architecture.dart'; class NotificationException extends AppException { - const NotificationException([super.message]); + const NotificationException([ + super.message, + this.failure, + ]); + + final Error? failure; } diff --git a/packages/wyatt_cloud_messaging_bloc_base/lib/src/core/helpers/app_exception_helper.dart b/packages/wyatt_cloud_messaging_bloc_base/lib/src/core/helpers/app_exception_helper.dart new file mode 100644 index 00000000..74d96648 --- /dev/null +++ b/packages/wyatt_cloud_messaging_bloc_base/lib/src/core/helpers/app_exception_helper.dart @@ -0,0 +1,26 @@ +// Copyright (C) 2023 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import 'package:wyatt_architecture/wyatt_architecture.dart'; +import 'package:wyatt_cloud_messaging_bloc_base/src/core/core.dart'; + +abstract final class AppExceptionHelper { + static AppException handleError(Object? error) => switch (error) { + AppException() => error, + Error() => NotificationException(error.toString(), error), + _ => ServerException(error.toString()), + }; +} diff --git a/packages/wyatt_cloud_messaging_bloc_base/lib/src/data/repositories/cloud_messaging_repository_impl.dart b/packages/wyatt_cloud_messaging_bloc_base/lib/src/data/repositories/cloud_messaging_repository_impl.dart index 4bc629d2..f5212d81 100644 --- a/packages/wyatt_cloud_messaging_bloc_base/lib/src/data/repositories/cloud_messaging_repository_impl.dart +++ b/packages/wyatt_cloud_messaging_bloc_base/lib/src/data/repositories/cloud_messaging_repository_impl.dart @@ -15,7 +15,7 @@ // along with this program. If not, see . import 'package:wyatt_architecture/wyatt_architecture.dart'; -import 'package:wyatt_cloud_messaging_bloc_base/src/core/exceptions/notification_exeption.dart'; +import 'package:wyatt_cloud_messaging_bloc_base/src/core/helpers/app_exception_helper.dart'; import 'package:wyatt_cloud_messaging_bloc_base/src/domain/data_sources/remote/cloud_messaging_remote_data_source.dart'; import 'package:wyatt_cloud_messaging_bloc_base/src/domain/entities/remote_notifications.dart'; import 'package:wyatt_cloud_messaging_bloc_base/src/domain/repositories/cloud_messaging_repository.dart'; @@ -31,68 +31,52 @@ class CloudMessagingRepositoryImpl extends CloudMessagingRepository { @override FutureOrResult getToken() => Result.tryCatchAsync( _notificationRemoteDataSource.getToken, - (error) => error is AppException - ? error - : NotificationException(error.toString()), + AppExceptionHelper.handleError, ); @override FutureOrResult subscribeToTopic(String topic) => Result.tryCatchAsync( () => _notificationRemoteDataSource.subscribeToTopic(topic), - (error) => error is AppException - ? error - : NotificationException(error.toString()), + AppExceptionHelper.handleError, ); @override FutureOrResult unsubscribeFromTopic(String topic) => Result.tryCatchAsync( () => _notificationRemoteDataSource.unsubscribeFromTopic(topic), - (error) => error is AppException - ? error - : NotificationException(error.toString()), + AppExceptionHelper.handleError, ); @override FutureOrResult requestPermissions() => Result.tryCatchAsync( _notificationRemoteDataSource.requestPermissions, - (error) => error is AppException - ? error - : NotificationException(error.toString()), + AppExceptionHelper.handleError, ); @override FutureOrResult> onNotification() => Result.tryCatchAsync( _notificationRemoteDataSource.onNotification, - (error) => error is AppException - ? error - : NotificationException(error.toString()), + AppExceptionHelper.handleError, ); @override FutureOrResult> onNotificationBackground() => Result.tryCatchAsync( _notificationRemoteDataSource.onNotificationBackground, - (error) => error is AppException - ? error - : NotificationException(error.toString()), + AppExceptionHelper.handleError, ); @override FutureOrResult> onNotificationForeground() => Result.tryCatchAsync( _notificationRemoteDataSource.onNotificationForeground, - (error) => error is AppException - ? error - : NotificationException(error.toString()), + AppExceptionHelper.handleError, ); @override FutureOrResult init() => Result.tryCatchAsync( _notificationRemoteDataSource.init, - (error) => error is AppException - ? error - : NotificationException(error.toString()), + AppExceptionHelper.handleError, ); } diff --git a/packages/wyatt_cloud_messaging_bloc_base/lib/src/domain/usecases/listen_notification_use_case.dart b/packages/wyatt_cloud_messaging_bloc_base/lib/src/domain/usecases/listen_notification_use_case.dart index c2fc004f..7c434046 100644 --- a/packages/wyatt_cloud_messaging_bloc_base/lib/src/domain/usecases/listen_notification_use_case.dart +++ b/packages/wyatt_cloud_messaging_bloc_base/lib/src/domain/usecases/listen_notification_use_case.dart @@ -40,7 +40,9 @@ class ListenNotificationUseCase if (notificationStreamResponse.isOk) { notificationStream = notificationStreamResponse.ok; } else if (notificationStreamResponse.isErr) { - if (notificationStreamResponse.err! is UnimplementedError) { + if (notificationStreamResponse.err! is NotificationException && + (notificationStreamResponse.err! as NotificationException).failure + is! UnimplementedError) { return Err(notificationStreamResponse.err!); }