withTracking<T> method

Future<T> withTracking<T>(
  1. String action,
  2. Future<T> body(), {
  3. Map<String, dynamic>? params,
})

Runs body, logging action at LogLevel.info on entry.

If body throws, the exception is recorded via trackError and re-thrown so the caller can still handle it.

Optional params are appended to the log message as key=value pairs.

Implementation

Future<T> withTracking<T>(
  String action,
  Future<T> Function() body, {
  Map<String, dynamic>? params,
}) async {
  final String message = (params != null && params.isNotEmpty)
      ? '$action: ${params.entries.map((e) => '${e.key}=${e.value}').join(', ')}'
      : action;
  await logger.info(message, context: trackerContext);
  try {
    return await body();
  } catch (e, st) {
    await trackError(e, stackTrace: st, message: 'Error during $action');
    rethrow;
  }
}