reportMessage static method

Future<void> reportMessage(
  1. String message, {
  2. ErrorSeverityLevel severityLevel = ErrorSeverityLevel.warning,
  3. StackTrace? stackTrace,
})

Reports a custom message with a corresponding severity and sometimes with a stack trace.

Useful in case you are handling errors that don't match reportError nor reportException.

Method might throw Exception.

try {
  // Call custom API.
} on CustomError catch (e, stackTrace) {
  await Instrumentation.reportMessage(
    e.toString(),
    severityLevel: ErrorSeverityLevel.info,
    stackTrace: stackTrace
  );
}

Implementation

static Future<void> reportMessage(String message,
    {ErrorSeverityLevel severityLevel = ErrorSeverityLevel.warning,
    StackTrace? stackTrace}) async {
  try {
    final hed = {
      "rst": stackTrace?.toString(),
      "crt": DateUtils.convertDateTimeToLong(DateTime.now()),
      "env": 'Flutter',
      "em": message,
    };

    final arguments = {
      "hed": jsonEncode(hed),
      "sev": severityLevel.index,
    };

    await channel.invokeMethod<void>('reportError', arguments);
  } on PlatformException catch (e) {
    throw Exception(e.details);
  }
}