An attempt to create an entity failed because it already exists
Indicates that a client tried to create a resource that already exists. This is a client-side error, and the client should not retry the creation.
- 1The client is trying to create a resource with an ID that is already in use.
- 2A previous request to create the same resource succeeded, but the client retried due to a network error.
- 3A race condition where two clients attempt to create the same resource simultaneously.
A client attempts to create a new user with an email address that is already registered.
// Client attempts to create a user with an existing ID
try {
await client.createUser({ userId: "existing-user-456", name: "Jane Doe" });
} catch (e) {
// e.code will be grpc.status.ALREADY_EXISTS
}expected output
StatusCode.ALREADY_EXISTS: An attempt to create an entity failed because it already exists
Fix 1
Implement 'Get-or-Create' Logic
WHEN When the client's intent is to ensure the resource exists.
try {
await client.createUser(request);
} catch (e) {
if (e.code === grpc.status.ALREADY_EXISTS) {
// It already exists, which is fine. Fetch it.
return await client.getUser({ userId: request.userId });
}
throw e;
}Why this works
Treating ALREADY_EXISTS as a successful outcome for a creation request makes the operation idempotent.
Fix 2
Provide User Feedback
WHEN When creating a resource via a user interface.
// In the UI, after catching ALREADY_EXISTS
displayErrorMessage("A user with this ID already exists. Please choose a different ID.");Why this works
Inform the user that their chosen identifier is already taken so they can correct it.
✕ Retry the creation request without modification
The resource already exists. Retrying the creation will fail every time and is a logical error.
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev