[backend/core] Improve handling of UniqueConstraintExceptions in UserService.CreateUserAsync

This commit is contained in:
Laura Hausmann 2024-07-13 16:28:47 +02:00
parent 9233e43b81
commit 030aef6c8d
No known key found for this signature in database
GPG key ID: D044E84C5BE01605

View file

@ -206,9 +206,9 @@ public class UserService(
}); });
return user; return user;
} }
catch (UniqueConstraintException) catch (UniqueConstraintException e) when (e.ConstraintProperties is [nameof(User.Uri)])
{ {
logger.LogDebug("Encountered UniqueConstraintException while creating user {uri}, attempting to refetch...", logger.LogError("Encountered UniqueConstraintException while creating user {uri}, attempting to refetch...",
user.Uri); user.Uri);
// another thread got there first, so we need to return the existing user // another thread got there first, so we need to return the existing user
var res = await db.Users var res = await db.Users
@ -226,6 +226,12 @@ public class UserService(
return res; return res;
} }
catch (UniqueConstraintException e)
{
logger.LogError("Failed to insert user: Unable to satisfy unique constraint: {constraint}",
e.ConstraintName);
throw;
}
} }
public async Task<User> UpdateUserAsync(string id) public async Task<User> UpdateUserAsync(string id)