sentry.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import 'dart:io';
  2. import 'package:flutter/services.dart';
  3. import 'package:sentry/sentry.dart';
  4. import 'package:device_info/device_info.dart';
  5. import 'cache.dart';
  6. enum ErrorExceptionEnum{
  7. /// dart异常
  8. RangeError,
  9. ///页面渲染异常
  10. FlutterErrorDetails,
  11. ///服务定义异常
  12. MissingPluginException,
  13. ///dio请求异常
  14. DioError
  15. }
  16. Map<String ,ErrorExceptionEnum> errorMap = {
  17. 'RangeError' : ErrorExceptionEnum.RangeError,
  18. 'FlutterErrorDetails' : ErrorExceptionEnum.FlutterErrorDetails,
  19. 'MissingPluginException' : ErrorExceptionEnum.MissingPluginException,
  20. 'DioError' : ErrorExceptionEnum.DioError,
  21. };
  22. class ErrorParsing {
  23. static dynamic errorStatus(dynamic error){
  24. ///判断是否是请求异常
  25. if(errorMap[error.runtimeType.toString()] == ErrorExceptionEnum.DioError){
  26. return {
  27. '\n msgData' :error?.response?.data??'',
  28. '\n 请求域名' :error?.request?.baseUrl ??'',
  29. '\n 请求类型' : error?.request?.method??'',
  30. '\n 请求地址' : error?.request?.path??'',
  31. '\n 错误' : '$error \n'
  32. };
  33. }
  34. return error;
  35. }
  36. static Map<String, dynamic> readAndroidBuildData(AndroidDeviceInfo build) {
  37. return <String, dynamic>{
  38. ' \n 最新补丁日期':'${build.version.securityPatch}',
  39. ' \n 操作系统名' : '${build.version.baseOS??'安卓'}',
  40. ' \n sdkInt':'${build.version.sdkInt}',
  41. '\n Android版本': '${build.version.release} ',
  42. '\n 手机品牌 ': '${build.brand} ',
  43. '\n 手机详细版本': '${build.model} ',
  44. '\n 外观设计名 ': '${build.device} ',
  45. '\n 版本号': '${build.display} ',
  46. '\n 当前手机唯一标识': '${build.fingerprint} ',
  47. '\n 内核(单词简写)': '${build.hardware} ',
  48. '\n 主机名 ': '${build.host} ',
  49. '\n id': '${build.id} ',
  50. '\n supported32BitAbis': '${build.supported32BitAbis} ',
  51. '\n supported64BitAbis': '${build.supported64BitAbis} ',
  52. '\n supportedAbis': '${build.supportedAbis} ',
  53. '\n 是否真机': '${build.isPhysicalDevice} \n',
  54. };
  55. }
  56. static Map<String, dynamic> readIosDeviceInfo(IosDeviceInfo data) {
  57. return <String, dynamic>{
  58. '\n 设备名': '${data.name} ',
  59. '\n 操作系统名': '${data.systemName} ',
  60. '\n 系统版本': '${data.systemVersion} ',
  61. '\n 设备型号': '${data.model} ',
  62. '\n 设备名(本地)': '${data.localizedModel}',
  63. '\n 当前设备唯一值': '${data.identifierForVendor} ',
  64. '\n 是否真机': '${data.isPhysicalDevice} ',
  65. '\n 版本号': '${data.utsname.version} ',
  66. '\n 硬件类型': '${data.utsname.machine} \n',
  67. };
  68. }
  69. }
  70. class SentryUtils {
  71. static SentryClient _sentry;
  72. static final DeviceInfoPlugin _plugin = DeviceInfoPlugin();
  73. static final _dsn = "http://82646cbdedbe446fa5d1edb62d505290@localhost:9090/1";
  74. static Future<Map<String, dynamic>> initPlatformState() async {
  75. Map<String, dynamic> deviceData;
  76. _sentry = new SentryClient(dsn: _dsn);
  77. try {
  78. if (Platform.isAndroid) {
  79. deviceData = ErrorParsing.readAndroidBuildData(await _plugin.androidInfo);
  80. } else if (Platform.isIOS) {
  81. deviceData = ErrorParsing.readIosDeviceInfo(await _plugin.iosInfo);
  82. }
  83. } on PlatformException {
  84. deviceData = <String, dynamic>{
  85. 'Error:': 'Failed to get platform version.'
  86. };
  87. }
  88. return deviceData;
  89. }
  90. static Future<Null> reportError(dynamic error, dynamic stackTrace) async {
  91. print("异常收集 error = ${error.toString()}");
  92. // print("error.type = ${error.runtimeType.toString()}");
  93. initPlatformState().then((res) {
  94. Map<String, dynamic> _errMap = {
  95. '\n 错误类型': error.runtimeType.toString(),
  96. '\n error': ErrorParsing.errorStatus(error),
  97. '\n 设备信息': res,
  98. '\n 用户信息': '${Cache.user.uid} \n '
  99. };
  100. _sentry.captureException(
  101. exception: _errMap,
  102. stackTrace: stackTrace,
  103. );
  104. }).catchError((err) {
  105. Map<String, dynamic> _errMap = {
  106. '\n 错误类型': error.runtimeType.toString(),
  107. '\n error': ErrorParsing.errorStatus(error),
  108. '\n 设备信息': '获取设备信息出错',
  109. '\n 用户信息': '${Cache.user.uid} \n '
  110. };
  111. _sentry.captureException(
  112. exception: _errMap,
  113. stackTrace: stackTrace,
  114. );
  115. });
  116. }
  117. }