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

新增自定义异常上报

上级 53876ac0
......@@ -46,6 +46,13 @@ public class HiBuglyPlugin implements FlutterPlugin, MethodCallHandler {
CrashReport.initCrashReport(flutterPluginBinding.getApplicationContext(), appID, debug);
result.success(true);
break;
case "postException":
String message = "";
if (call.hasArgument("message")) {
message = call.argument("message");
}
CrashReport.postCatchedException(new Throwable(message));
break;
default:
result.notImplemented();
break;
......
......@@ -30,8 +30,9 @@ class _MyAppState extends State<MyApp> {
body: Column(
children: [
MaterialButton(
onPressed: () {
HiBugly.initialize(appID: '123456', debug: true);
onPressed: () async {
await HiBugly.initialize(appID: '123456', debug: true);
HiBugly.postException(name: '异常名', reason: '异常原因', callStack: '异常堆栈', extraInfo: '额外信息');
},
child: const Text('初始化'),
),
......
......@@ -11,16 +11,34 @@
}
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"initialize" isEqualToString:call.method]) {
NSString *appID = call.arguments[@"appID"];
BOOL debug = call.arguments[@"debug"];
BuglyConfig *config = [[BuglyConfig alloc] init];
config.debugMode = debug;
[Bugly startWithAppId:appID config: config];
result([NSNumber numberWithBool:YES]);
} else {
result(FlutterMethodNotImplemented);
}
NSString *method = call.method;
if ([method isEqualToString:@"initialize"]) {
NSString *appID = call.arguments[@"appID"];
BOOL debug = call.arguments[@"debug"];
BuglyConfig *config = [[BuglyConfig alloc] init];
config.debugMode = debug;
[Bugly startWithAppId:appID config: config];
result([NSNumber numberWithBool:YES]);
}
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
......@@ -10,4 +10,18 @@ class HiBugly {
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/services.dart';
......@@ -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 {
}) {
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
// TODO: implement initialize
throw UnimplementedError();
}
@override
void postException({String name = '', String reason = '', String callStack = '', String extraInfo = ''}) {
// TODO: implement postException
throw UnimplementedError();
}
}
void main() {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论