Improve user update error handling for duplicate entries

Add specific error handling for duplicate username or email during user updates, returning 409 status codes and informative messages.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: a8a78c07-e900-425c-a577-5b4c5894379d
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/a8a78c07-e900-425c-a577-5b4c5894379d/vetH0Gp
This commit is contained in:
michaelschal committed 2025-08-11 18:26:35 +00:00
1 parent 98a3ae5009
commit d85d62a5da
3 files changed
+199 -1

No files matched your search

@@ -0,0 +1,182 @@
🔗 Database initialization: {
NODE_ENV: 'production',
isProduction: true,
hasDbUrl: true,
dbHost: 'logiflow-db'
}
🐳 PRODUCTION: Using standard PostgreSQL
✅ PostgreSQL connection test successful
🐳 PRODUCTION: Using PostgreSQL storage
🐳 PRODUCTION: Local auth configured with PostgreSQL sessions
🐳 PRODUCTION: Starting LogiFlow application
🐳 Environment: { NODE_ENV: 'production', DATABASE_URL: 'Present', PORT: '3000' }
🔍 Using development storage and authentication
🔧 Using memory session store for development
Warning: connect.session() MemoryStore is not
designed for a production environment, as it will leak
memory, and will not scale past a single process.
🐳 Serving static files from: /app/dist/public
🐳 PRODUCTION: LogiFlow serving on port 3000
✅ Production admin user found: { id: 'admin_local', username: 'admin', passwordFormat: 'present' }
🔐 Production password comparison { suppliedLength: 5, storedFormat: '63d2ce518b...' }
✅ Password matched with hash.salt format
✅ Admin password works with current system
HEAD /api/health 200 in 5ms
Error updating user: error: duplicate key value violates unique constraint "users_email_key"
at /app/node_modules/pg-pool/index.js:45:11
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async file:///app/node_modules/drizzle-orm/node-postgres/session.js:83:22
at async DatabaseStorage.updateUser (file:///app/dist/index.js:1085:24)
at async file:///app/dist/index.js:4051:27 {
length: 210,
severity: 'ERROR',
code: '23505',
detail: 'Key (email)=(admin@logiflow.com) already exists.',
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: 'public',
table: 'users',
column: undefined,
dataType: undefined,
constraint: 'users_email_key',
file: 'nbtinsert.c',
line: '664',
routine: '_bt_check_unique'
}
PUT /api/users/_1754936203292 500 in 96ms
HEAD /api/health 200 in 1ms
HEAD /api/health 200 in 0ms
HEAD /api/health 200 in 1ms
HEAD /api/health 200 in 1ms
HEAD /api/health 200 in 1ms
HEAD /api/health 200 in 16ms
HEAD /api/health 200 in 1ms
HEAD /api/health 200 in 4ms
HEAD /api/health 200 in 1ms
HEAD /api/health 200 in 1ms
HEAD /api/health 200 in 1ms
HEAD /api/health 200 in 1ms
HEAD /api/health 200 in 1ms
HEAD /api/health 200 in 1ms
HEAD /api/health 200 in 1ms
HEAD /api/health 200 in 3ms
Error updating user: error: duplicate key value violates unique constraint "users_email_key"
at /app/node_modules/pg-pool/index.js:45:11
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async file:///app/node_modules/drizzle-orm/node-postgres/session.js:83:22
at async DatabaseStorage.updateUser (file:///app/dist/index.js:1085:24)
at async file:///app/dist/index.js:4051:27 {
length: 210,
severity: 'ERROR',
code: '23505',
detail: 'Key (email)=(admin@logiflow.com) already exists.',
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: 'public',
table: 'users',
column: undefined,
dataType: undefined,
constraint: 'users_email_key',
file: 'nbtinsert.c',
line: '664',
routine: '_bt_check_unique'
}
PUT /api/users/_1754936203292 500 in 44ms
HEAD /api/health 200 in 1ms
Binary file not shown.

After

Width:  |  Height:  |  Size: 295 KiB

+17 -1
View File
@@ -954,11 +954,27 @@ export async function registerRoutes(app: Express): Promise<Server> {
const updatedUser = await storage.updateUser(req.params.id, userData);
res.json(updatedUser);
} catch (error) {
} catch (error: any) {
console.error("Error updating user:", error);
if (error instanceof z.ZodError) {
return res.status(400).json({ message: "Invalid user data", errors: error.errors });
}
// Handle specific database constraint errors
if (error.code === '23505') {
if (error.constraint === 'users_username_key') {
return res.status(409).json({
message: "Un utilisateur avec ce nom d'utilisateur existe déjà. Veuillez choisir un autre nom d'utilisateur."
});
}
if (error.constraint === 'users_email_key') {
return res.status(409).json({
message: "Un utilisateur avec cette adresse email existe déjà. Veuillez utiliser une autre adresse email ou laisser le champ vide."
});
}
}
res.status(500).json({ message: "Failed to update user" });
}
});