Skip to content

Commit

Permalink
Merge #32 and improve a few things
Browse files Browse the repository at this point in the history
  • Loading branch information
ThexXTURBOXx committed May 20, 2024
1 parent 63dfb68 commit 8fafc45
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 44 deletions.
43 changes: 21 additions & 22 deletions lib/core/catcher_2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Catcher2 implements ReportModeAction {

_setupErrorHooks();

_initWidgetBindingAndRunApp();
_initWidgetsBindingAndRunApp();

// Loading device and application info requires that the widgets binding is
// initialized so we need to run it after we init WidgetsFlutterBinding.
Expand Down Expand Up @@ -184,17 +184,15 @@ class Catcher2 implements ReportModeAction {
};

// PlatformDispatcher.instance.onError catches ASYNCHRONOUS errors, but it
// does not work for web, most likely due to this issue:
// currently does not work for Web, most likely due to this issue:
// https://github.com/flutter/flutter/issues/100277
if (!kIsWeb) {
PlatformDispatcher.instance.onError = (error, stack) {
_reportError(error, stack);
_currentConfig.onPlatformError?.call(error, stack);
return true;
};
}
PlatformDispatcher.instance.onError = (error, stack) {
_reportError(error, stack);
_currentConfig.onPlatformError?.call(error, stack);
return true;
};

/// Web doesn't have Isolate error listener support
// Web doesn't have Isolate error listener support
if (!kIsWeb) {
Isolate.current.addErrorListener(
RawReceivePort((pair) async {
Expand All @@ -208,21 +206,17 @@ class Catcher2 implements ReportModeAction {
}
}

void _initWidgetBindingAndRunApp() {
if (!kIsWeb) {
// This isn't web, we can just run the app, no need for runZoneGuarded
// since async errors are caught by PlatformDispatcher.instance.onError.
_initWidgetsBindingIfNeeded();
_runApp();
} else {
// We are in a web environment so we need runZoneGuarded to catch async
// exceptions.
void _initWidgetsBindingAndRunApp() {
if (kIsWeb) {
// Due to https://github.com/flutter/flutter/issues/100277
// this is still needed... As soon as proper error catching support
// for Web is implemented, this branch should be merged with the other.
unawaited(
runZonedGuarded<Future<void>>(
() async {
// It is important that we run init widgets binding inside the
// runZonedGuarded call to be able to catch the async execeptions.
_initWidgetsBindingIfNeeded();
// runZonedGuarded call to be able to catch the async exceptions.
_initWidgetsBinding();
_runApp();
},
(error, stack) {
Expand All @@ -231,6 +225,11 @@ class Catcher2 implements ReportModeAction {
},
),
);
} else {
// This isn't Web, we can just run the app, no need for runZoneGuarded
// since async errors are caught by PlatformDispatcher.instance.onError.
_initWidgetsBinding();
_runApp();
}
}

Expand All @@ -244,7 +243,7 @@ class Catcher2 implements ReportModeAction {
}
}

void _initWidgetsBindingIfNeeded() {
void _initWidgetsBinding() {
if (ensureInitialized) {
WidgetsFlutterBinding.ensureInitialized();
}
Expand Down
6 changes: 4 additions & 2 deletions lib/handlers/http_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ class HttpHandler extends ReportHandler {
Response<dynamic>? response;
_printLog('Calling: $endpointUri');
if (report.screenshot != null) {
final screenshotPath = report.screenshot?.path ?? '';
final formData = FormData.fromMap(<String, dynamic>{
'payload_json': json,
'file': await MultipartFile.fromFile(screenshotPath),
'file': MultipartFile.fromBytes(
await report.screenshot!.readAsBytes(),
filename: report.screenshot!.name,
),
});
response = await _dio.post<dynamic>(
endpointUri.toString(),
Expand Down
33 changes: 13 additions & 20 deletions lib/handlers/sentry_handler.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:io';

import 'package:catcher_2/model/platform_type.dart';
import 'package:catcher_2/model/report.dart';
import 'package:catcher_2/model/report_handler.dart';
Expand All @@ -10,9 +8,9 @@ import 'package:sentry/sentry.dart';
class SentryHandler extends ReportHandler {
SentryHandler(
this.sentryClient, {
this.userContext,
this.serverName = 'Catcher 2',
this.loggerName = 'Catcher 2',
this.userContext,
this.enableDeviceParameters = true,
this.enableApplicationParameters = true,
this.enableCustomParameters = true,
Expand All @@ -23,12 +21,16 @@ class SentryHandler extends ReportHandler {

/// Sentry Client instance
final SentryClient sentryClient;
final String serverName;
final String loggerName;

/// User data
SentryUser? userContext;

/// The server name to send
final String serverName;

/// The logger name to send
final String loggerName;

/// Enable device parameters to be generated by Catcher 2
final bool enableDeviceParameters;

Expand All @@ -38,15 +40,15 @@ class SentryHandler extends ReportHandler {
/// Enable custom parameters to be generated by Catcher 2
final bool enableCustomParameters;

/// Custom environment, if null, Catcher 2 will generate it
/// Enable additional logs printing
final bool printLogs;

/// Custom environment; if `null`, Catcher 2 will generate it
final String? customEnvironment;

/// Custom release, if null, Catcher 2 will generate it
/// Custom release; if `null`, Catcher 2 will generate it
final String? customRelease;

/// Enable additional logs printing
final bool printLogs;

@override
Future<bool> handle(Report report, BuildContext? context) async {
try {
Expand All @@ -70,12 +72,9 @@ class SentryHandler extends ReportHandler {
// and the code relies on File from dart:io that does not work in web
// either because we do not have access to the file system in web.
SentryAttachment? screenshotAttachment;
File? screenshotFile;
try {
if (report.screenshot != null && !kIsWeb) {
final screenshotPath = report.screenshot!.path;
screenshotFile = File(screenshotPath);
final bytes = await screenshotFile.readAsBytes();
final bytes = await report.screenshot!.readAsBytes();
screenshotAttachment = SentryAttachment.fromScreenshotData(bytes);
_printLog('Created screenshot attachment');
}
Expand All @@ -91,12 +90,6 @@ class SentryHandler extends ReportHandler {
: null,
);

if (screenshotFile != null) {
// Cleanup screenshot file after submission to save space on device.
await screenshotFile.delete();
_printLog('Screenshot file removed from device (cleanup)');
}

_printLog('Logged to sentry!');
return true;
} catch (exception, stackTrace) {
Expand Down

0 comments on commit 8fafc45

Please sign in to comment.