How to handle database errors

2025-03-04T22:01:39.315Z ERROR [Better Auth]: error error: duplicate key value violates unique constraint "user_phoneNumber_key" at async POST (src/app/api/auth/[...all]/route.ts:8:14) at async POST (src/app/api/auth/[...all]/route.ts:8:14) 6 | 7 | export const POST = async (req: NextRequest) => {
8 | const res = await auth.handler(req);
| ^ 9 | return res; 10 | }; 11 | { length: 221, severity: 'ERROR', code: '23505', detail: 'Key ("phoneNumber")=(+1(phonenumber)) already exists.', hint: undefined, position: undefined, internalPosition: undefined, internalQuery: undefined, where: undefined, schema: 'public', table: 'user', column: undefined, dataType: undefined, constraint: 'user_phoneNumber_key', file: 'nbtinsert.c', line: '666', routine: '_bt_check_unique' }
9 Replies
quickFastJoe
quickFastJoeOP2mo ago
How can I catch this error?
bekacru
bekacru2mo ago
when does this happen?
quickFastJoe
quickFastJoeOP2mo ago
basically i verify users phone number via otp if an account already has the number allocated i get this error i would like to catch it so i can properly handle the error I tried hooks using ctx.context.returned but cause its 500 before and after i dont seem to get it
bekacru
bekacru2mo ago
wait so you made identifier in verification table unique?
quickFastJoe
quickFastJoeOP2mo ago
This seems to be referencing the user table i did not change anything in the verification table schema: 'public', table: 'user', const verifyResponse = await authClient.phoneNumber.verify({ phoneNumber: phoneNumber, code: values.pin, updatePhoneNumber: true }); this is the call
bekacru
bekacru2mo ago
if a user already exist with the phone number, it should throw a proper error. I'll check if anything has changed.
quickFastJoe
quickFastJoeOP2mo ago
im using update phonenumber think it might be that
SKU
SKU2mo ago
Any updates w this also have this issue
bekacru
bekacru2mo ago
will be fixed on next release

Did you find this page helpful?