guarded<T> method

Future<T> guarded<T>(
  1. Future<T> body(), {
  2. bool fatal = false,
})

Runs body and automatically logs any thrown error.

Simpler than withTracking — no action name is required. Use this when you only need error protection without action logging.

Future<void> fetchUser(String id) => guarded(() async {
  final data = await api.getUser(id);
  setState(() => _user = data);
});

The error is re-thrown after recording so the caller can still handle it.

Implementation

Future<T> guarded<T>(Future<T> Function() body, {bool fatal = false}) async {
  try {
    return await body();
  } catch (e, st) {
    await trackError(e, stackTrace: st, fatal: fatal);
    rethrow;
  }
}