✅ Design of error handling for database calls (dapper, .net core)

Hello, Writing an app that uses DB calls (from dapper) and wondering how most people like to see error handling for these. Currently I have:
DailyLogBeam? dailyLogInfo = null;
try
{
dailyLogInfo = await _db.GetOne<DailyLogBeam, dynamic, SqlConnection>(sql, new { shift = shift.StartTime }, "Meltcast");
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to fetch log info!");
}

if (dailyLogInfo is null)
{
//initializing empty db result
}

// Continued execution with db results
DailyLogBeam? dailyLogInfo = null;
try
{
dailyLogInfo = await _db.GetOne<DailyLogBeam, dynamic, SqlConnection>(sql, new { shift = shift.StartTime }, "Meltcast");
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to fetch log info!");
}

if (dailyLogInfo is null)
{
//initializing empty db result
}

// Continued execution with db results
2 Replies
Mayor McCheese
Mayor McCheese16mo ago
I don’t generally catch Exception other than at the top level because it tends to hide other problems, instead I catch specific exception types sometimes with additional pattern matching; but it’s an opinionated area, and nuanced to specific needs.
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.