From 1dc37d328240804440015facbca4cfe6c7274a4d Mon Sep 17 00:00:00 2001 From: martinshoob Date: Mon, 11 Aug 2025 18:46:35 +0200 Subject: [PATCH] Handle not found user profile gracefully Changes from throwing an exception when a user profile is not found to returning null. This prevents the application from crashing when a user profile is not found, allowing for more graceful error handling. --- DrinkRateAPI/Services/ApplicationUserService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DrinkRateAPI/Services/ApplicationUserService.cs b/DrinkRateAPI/Services/ApplicationUserService.cs index a2f735d..7df4ac0 100644 --- a/DrinkRateAPI/Services/ApplicationUserService.cs +++ b/DrinkRateAPI/Services/ApplicationUserService.cs @@ -13,7 +13,7 @@ public class ApplicationUserService(ApplicationDbContext context) { var appUserId = identity.FindFirst(ClaimTypes.NameIdentifier)?.Value; var profile = await _context.UserProfiles - .FirstAsync(x => x.ApplicationUserId.ToString() == appUserId) + .FirstOrDefaultAsync(x => x.ApplicationUserId.ToString() == appUserId) ?? throw new NotFoundException(); return profile;