tryRecordException method

Future<void> tryRecordException({
  1. required Exception exception,
  2. StackTrace? stack,
})

Attempts to log an exception to an external monitoring service.

If onErrorRecorded is provided in the constructor, the exception will be passed to that callback (ideal for unit testing or custom logging).

Otherwise, it defaults to logging via Firebase Crashlytics with the 'df_http' tag. Failures during the logging process itself are caught and printed to the Logger to prevent the app from crashing while trying to report an error.

Parameters:

  • exception: The error object encountered during the API call.
  • stack: The stack trace associated with the error for easier debugging.

Implementation

Future<void> tryRecordException({
  required Exception exception,
  StackTrace? stack,
}) async {
  if (onErrorRecorded != null) {
    await onErrorRecorded!(exception, stack);
  } else {
    try {
      await FirebaseCrashlytics.instance.recordError(
        exception,
        stack,
        reason: 'a non-fatal error',
        information: ['df_http'],
      );
    } catch (e) {
      Logger.log(
        "Failed logging to firebase crashlytics: $e",
        type: LogType.warning,
      );
    }
  }
}