diff --git a/lib/core/context/tb_context.dart b/lib/core/context/tb_context.dart index 93cd0690e..fe0a7aac3 100644 --- a/lib/core/context/tb_context.dart +++ b/lib/core/context/tb_context.dart @@ -18,6 +18,7 @@ import 'package:thingsboard_app/utils/services/endpoint/i_endpoint_service.dart' import 'package:thingsboard_app/utils/services/firebase/i_firebase_service.dart'; import 'package:thingsboard_app/utils/services/notification_service.dart'; import 'package:thingsboard_app/utils/services/overlay_service/i_overlay_service.dart'; +import 'package:thingsboard_app/utils/translation_utils.dart'; import 'package:thingsboard_app/utils/utils.dart'; import 'package:universal_platform/universal_platform.dart'; @@ -117,29 +118,15 @@ class TbContext implements PopEntry { } Future onFatalError(dynamic e) async { - String getMessage(dynamic e, BuildContext context) { - final message = - e is ThingsboardError - ? (e.message ?? S.of(context).unknownError) - : S.of(context).unknownError; - - return '${S.of(context).fatalApplicationErrorOccurred}\n$message'; - } - await _overlayService.showAlertDialog( - content: - (context) => DialogContent( - title: S.of(context).fatalError, - message: getMessage(e, context), - ok: S.of(context).cancel, - ), + content: (context) => fatalErrorDialogContent(context, e), ); logout(); } void onError(ThingsboardError tbError) { log.error('onError', tbError, tbError.getStackTrace()); - _overlayService.showErrorNotification((_) => tbError.message!); + _overlayService.showErrorNotification(tbError.getTranslatedMessage); } void onLoadStarted() { diff --git a/lib/utils/providers/error_provider/error_provider.dart b/lib/utils/providers/error_provider/error_provider.dart index b640d7d01..7901c1699 100644 --- a/lib/utils/providers/error_provider/error_provider.dart +++ b/lib/utils/providers/error_provider/error_provider.dart @@ -1,11 +1,10 @@ -import 'package:flutter/material.dart'; import 'package:riverpod_annotation/riverpod_annotation.dart'; import 'package:thingsboard_app/core/auth/login/provider/login_provider.dart'; import 'package:thingsboard_app/core/logger/tb_logger.dart'; -import 'package:thingsboard_app/generated/l10n.dart'; import 'package:thingsboard_app/locator.dart'; import 'package:thingsboard_app/thingsboard_client.dart'; import 'package:thingsboard_app/utils/services/overlay_service/i_overlay_service.dart'; +import 'package:thingsboard_app/utils/translation_utils.dart'; part 'error_provider.g.dart'; @riverpod @@ -19,25 +18,12 @@ class Error extends _$Error { void onError(ThingsboardError tbError) { _log.error('onError', tbError, tbError.getStackTrace()); - _overlayService.showErrorNotification((_) => tbError.message!); + _overlayService.showErrorNotification(tbError.getTranslatedMessage); } - String _getMessage(dynamic e, BuildContext context) { - final message = - e is ThingsboardError - ? (e.message ?? S.of(context).unknownError) - : S.of(context).unknownError; - - return '${S.of(context).fatalApplicationErrorOccurred}\n$message'; - } Future onFatalError(dynamic e) async { await _overlayService.showAlertDialog( - content: - (context) => DialogContent( - title: S.of(context).fatalError, - message: _getMessage(e, context), - ok: S.of(context).cancel, - ), + content: (context) => fatalErrorDialogContent(context, e), ); ref.read(loginProvider.notifier).logout(); } diff --git a/lib/utils/services/tb_client_service/tb_client_service.dart b/lib/utils/services/tb_client_service/tb_client_service.dart index fba123745..dc22bcd55 100644 --- a/lib/utils/services/tb_client_service/tb_client_service.dart +++ b/lib/utils/services/tb_client_service/tb_client_service.dart @@ -10,6 +10,7 @@ import 'package:thingsboard_app/utils/services/endpoint/i_endpoint_service.dart' import 'package:thingsboard_app/utils/services/loading_service/i_loading_service.dart'; import 'package:thingsboard_app/utils/services/overlay_service/i_overlay_service.dart'; import 'package:thingsboard_app/utils/services/tb_client_service/i_tb_client_service.dart'; +import 'package:thingsboard_app/utils/translation_utils.dart'; import 'package:thingsboard_app/utils/utils.dart'; import 'package:thingsboard_app/thingsboard_client.dart'; @@ -54,23 +55,9 @@ class TbClientService implements ITbClientService { getIt().fire(const UserLoadedEvent()); } - String _getMessage(dynamic e, BuildContext context) { - final message = - e is ThingsboardError - ? (e.message ?? S.of(context).unknownError) - : S.of(context).unknownError; - - return '${S.of(context).fatalApplicationErrorOccurred}\n$message'; - } - void onInitError(dynamic e) { _overlayService.showAlertDialog( - content: - (context) => DialogContent( - title: S.of(context).fatalError, - message: _getMessage(e, context), - ok: S.of(context).cancel, - ), + content: (context) => fatalErrorDialogContent(context, e), ); } @@ -88,7 +75,7 @@ class TbClientService implements ITbClientService { return; } - _overlayService.showErrorNotification((_) => e.message!); + _overlayService.showErrorNotification(e.getTranslatedMessage); }); } diff --git a/lib/utils/translation_utils.dart b/lib/utils/translation_utils.dart index 3405649f6..d58e83843 100644 --- a/lib/utils/translation_utils.dart +++ b/lib/utils/translation_utils.dart @@ -166,5 +166,29 @@ extension ActionTypeTranslationUtils on ActionType { } } +extension ThingsboardErrorTranslationUtils on ThingsboardError { + /// App-wide fallback text for a message-less [ThingsboardError]; the wording + /// mirrors the client's own "Unknown error" literal in `parseMessage`. + String getTranslatedMessage(BuildContext context) { + final message = this.message; + return message == null || message.isEmpty + ? S.of(context).unknownError + : message; + } +} + +DialogContent fatalErrorDialogContent(BuildContext context, Object? error) { + final message = + error is ThingsboardError + ? error.getTranslatedMessage(context) + : S.of(context).unknownError; + + return DialogContent( + title: S.of(context).fatalError, + message: '${S.of(context).fatalApplicationErrorOccurred}\n$message', + ok: S.of(context).cancel, + ); +} + typedef TranslationBuilder = String Function(BuildContext context); typedef TranslatedDialogBuilder = DialogContent Function(BuildContext context);