Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions lib/core/context/tb_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -117,29 +118,15 @@ class TbContext implements PopEntry {
}

Future<void> 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() {
Expand Down
20 changes: 3 additions & 17 deletions lib/utils/providers/error_provider/error_provider.dart
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<void> 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();
}
Expand Down
19 changes: 3 additions & 16 deletions lib/utils/services/tb_client_service/tb_client_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -54,23 +55,9 @@ class TbClientService implements ITbClientService {
getIt<ICommunicationService>().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),
);
}

Expand All @@ -88,7 +75,7 @@ class TbClientService implements ITbClientService {

return;
}
_overlayService.showErrorNotification((_) => e.message!);
_overlayService.showErrorNotification(e.getTranslatedMessage);
});
}

Expand Down
24 changes: 24 additions & 0 deletions lib/utils/translation_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);