diff --git a/lib/main.dart b/lib/main.dart index 193240518..541415f13 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,7 @@ import 'dart:async'; +import 'dart:convert'; +import 'dart:io'; +import 'dart:isolate'; import 'package:cake_wallet/bitcoin/bitcoin.dart'; import 'package:cake_wallet/entities/language_service.dart'; import 'package:cake_wallet/buy/order.dart'; @@ -46,11 +49,24 @@ Future main() async { await runZonedGuarded(() async { WidgetsFlutterBinding.ensureInitialized(); - // FlutterError.onError = ; + FlutterError.onError = (errorDetails) { + _saveException(errorDetails.exception.toString(), errorDetails.stack); + }; - // Isolate.current.addErrorListener(RawReceivePort((pair) async { - // final List errorAndStacktrace = pair; - // }).sendPort); + PlatformDispatcher.instance.onError = (error, stack) { + _saveException(error.toString(), stack); + return true; + }; + + Isolate.current.addErrorListener(RawReceivePort((pair) async { + final errorAndStacktrace = pair as List; + _saveException( + errorAndStacktrace.first, + errorAndStacktrace.last == null + ? null + : StackTrace.fromString(errorAndStacktrace.last!), + ); + }).sendPort); final appDir = await getApplicationDocumentsDirectory(); await Hive.close(); @@ -151,6 +167,17 @@ Future main() async { }); } +void _saveException(String? error, StackTrace? stackTrace) async { + final file = File('/error.txt'); + final exception = { + "${DateTime.now()}": { + "error": error, + "stackTrace": stackTrace.toString(), + } + }; + await file.writeAsString(jsonEncode(exception), mode: FileMode.append); +} + Future initialSetup( {required SharedPreferences sharedPreferences, required Box nodes,