提交 3a03bab4 authored 作者: jiangjun's avatar jiangjun

新增自定义异常上报

上级 53876ac0
...@@ -46,6 +46,13 @@ public class HiBuglyPlugin implements FlutterPlugin, MethodCallHandler { ...@@ -46,6 +46,13 @@ public class HiBuglyPlugin implements FlutterPlugin, MethodCallHandler {
CrashReport.initCrashReport(flutterPluginBinding.getApplicationContext(), appID, debug); CrashReport.initCrashReport(flutterPluginBinding.getApplicationContext(), appID, debug);
result.success(true); result.success(true);
break; break;
case "postException":
String message = "";
if (call.hasArgument("message")) {
message = call.argument("message");
}
CrashReport.postCatchedException(new Throwable(message));
break;
default: default:
result.notImplemented(); result.notImplemented();
break; break;
......
...@@ -30,8 +30,9 @@ class _MyAppState extends State<MyApp> { ...@@ -30,8 +30,9 @@ class _MyAppState extends State<MyApp> {
body: Column( body: Column(
children: [ children: [
MaterialButton( MaterialButton(
onPressed: () { onPressed: () async {
HiBugly.initialize(appID: '123456', debug: true); await HiBugly.initialize(appID: '123456', debug: true);
HiBugly.postException(name: '异常名', reason: '异常原因', callStack: '异常堆栈', extraInfo: '额外信息');
}, },
child: const Text('初始化'), child: const Text('初始化'),
), ),
......
...@@ -11,16 +11,34 @@ ...@@ -11,16 +11,34 @@
} }
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"initialize" isEqualToString:call.method]) { NSString *method = call.method;
NSString *appID = call.arguments[@"appID"]; if ([method isEqualToString:@"initialize"]) {
BOOL debug = call.arguments[@"debug"]; NSString *appID = call.arguments[@"appID"];
BuglyConfig *config = [[BuglyConfig alloc] init]; BOOL debug = call.arguments[@"debug"];
config.debugMode = debug; BuglyConfig *config = [[BuglyConfig alloc] init];
[Bugly startWithAppId:appID config: config]; config.debugMode = debug;
result([NSNumber numberWithBool:YES]); [Bugly startWithAppId:appID config: config];
} else { result([NSNumber numberWithBool:YES]);
result(FlutterMethodNotImplemented); }
} else if ([method isEqualToString:@"postException"]) {
NSString *name = call.arguments[@"name"];
NSString *reason = call.arguments[@"reason"];
// 此调用栈为 dart 调用栈,放入扩展信息
NSString *callStack = call.arguments[@"callStack"];
NSString *extraInfo = call.arguments[@"extraInfo"];
[Bugly reportExceptionWithCategory: 3
name:name
reason:reason
callStack:@[]
extraInfo:@{
@"dartCallStack" : callStack,
@"extraInfo": extraInfo,
}
terminateApp:NO];
}
else {
result(FlutterMethodNotImplemented);
}
} }
@end @end
...@@ -10,4 +10,18 @@ class HiBugly { ...@@ -10,4 +10,18 @@ class HiBugly {
debug: debug, debug: debug,
); );
} }
static postException({
String name = '',
String reason = '',
String callStack = '',
String extraInfo = '',
}) {
HiBuglyPlatform.instance.postException(
name: name,
reason: reason,
callStack: callStack,
extraInfo: extraInfo,
);
}
} }
import 'dart:io';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
...@@ -22,4 +24,32 @@ class MethodChannelHiBugly extends HiBuglyPlatform { ...@@ -22,4 +24,32 @@ class MethodChannelHiBugly extends HiBuglyPlatform {
}, },
); );
} }
@override
void postException({
String name = '',
String reason = '',
String callStack = '',
String extraInfo = '',
}) {
if (Platform.isAndroid) {
final message = {
'name': name,
'reason': reason,
'callStack': callStack,
'extraInfo': extraInfo,
};
methodChannel.invokeMethod(
'postException',
{'message': message.toString()},
);
} else {
methodChannel.invokeMethod('postException', {
'name': name,
'reason': reason,
'callStack': callStack,
'extraInfo': extraInfo,
});
}
}
} }
...@@ -29,4 +29,13 @@ abstract class HiBuglyPlatform extends PlatformInterface { ...@@ -29,4 +29,13 @@ abstract class HiBuglyPlatform extends PlatformInterface {
}) { }) {
throw UnimplementedError('initialize() has not been implemented.'); throw UnimplementedError('initialize() has not been implemented.');
} }
void postException({
String name = '',
String reason = '',
String callStack = '',
String extraInfo = '',
}) {
throw UnimplementedError('postException() has not been implemented.');
}
} }
...@@ -14,6 +14,12 @@ class MockHiBuglyPlatform ...@@ -14,6 +14,12 @@ class MockHiBuglyPlatform
// TODO: implement initialize // TODO: implement initialize
throw UnimplementedError(); throw UnimplementedError();
} }
@override
void postException({String name = '', String reason = '', String callStack = '', String extraInfo = ''}) {
// TODO: implement postException
throw UnimplementedError();
}
} }
void main() { void main() {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论