Violating database constraints—like trying to update a user's email to one that already exists—causes the application to crash.
Should we wrap database queries in a try/catch block to prevent this?
In any case, we should update the user edit form to catch this specific case and prevent the app from crashing when a duplicate email is entered.
async update({ request, response, bouncer, params }: HttpContext) {
const user = await db()
.selectFrom('users')
.where('id', '=', params.id)
.select(['id'])
.executeTakeFirstOrThrow()
await bouncer.with(UserPolicy).authorize('edit', user)
const data = await request.validateUsing(updateUserValidator)
if (data.password) data.password = await hash.make(data.password)
+ try {
await db()
.updateTable('users')
.where('id', '=', params.id)
.set({
...data,
updatedAt: new Date(),
})
.execute()
+ catch(DatabaseError) {
+ }
return response.redirect('/users')
}
Violating database constraints—like trying to update a user's email to one that already exists—causes the application to crash.
Should we wrap database queries in a try/catch block to prevent this?
In any case, we should update the user edit form to catch this specific case and prevent the app from crashing when a duplicate email is entered.
async update({ request, response, bouncer, params }: HttpContext) { const user = await db() .selectFrom('users') .where('id', '=', params.id) .select(['id']) .executeTakeFirstOrThrow() await bouncer.with(UserPolicy).authorize('edit', user) const data = await request.validateUsing(updateUserValidator) if (data.password) data.password = await hash.make(data.password) + try { await db() .updateTable('users') .where('id', '=', params.id) .set({ ...data, updatedAt: new Date(), }) .execute() + catch(DatabaseError) { + } return response.redirect('/users') }