Resl1009 Migrate Trycatch To Resultt.Try Info Code Fix
title: RESL1009 — Migrate try/catch to Result<T>.Try [Info + Code Fix]
// ℹ️ RESL1009: This try/catch pattern can be replaced with 'Result<User>.Try(() => ...)'
// 💡 Fix A: Result<User>.Try(() => expr)
// 💡 Fix B: Result<User>.Try(() => expr, ex => new DatabaseError(ex.Message))
private Result<User> LoadUser(int id)
{
try
{
return repository.GetById(id); // ← safe: no Result<T> in try body
}
catch (Exception ex)
{
return Result<User>.Fail(new DatabaseError(ex.Message));
}
}
// ✅ Fix A — minimal rewrite (ExceptionError wraps any exception):
private Result<User> LoadUser(int id) =>
Result<User>.Try(() => repository.GetById(id));
// ✅ Fix B — custom error handler preserved:
private Result<User> LoadUser(int id) =>
Result<User>.Try(() => repository.GetById(id), ex => new DatabaseError(ex.Message));
Async methods are also handled — async/await is removed and TryAsync is used:
// Before (fires RESL1009):
private async Task<Result<User>> LoadUserAsync(int id)
{
try { return await repository.GetByIdAsync(id); }
catch (Exception ex) { return Result<User>.Fail(new DatabaseError(ex.Message)); }
}
// ✅ Fix A — async rewrite:
private Task<Result<User>> LoadUserAsync(int id) =>
Result<User>.TryAsync(() => repository.GetByIdAsync(id));
Does NOT fire when: catch catches a specific exception type (catch (SqlException)), the try body already returns Result<T>, a finally block is present, or the method does not return Result<T>.