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
How can I catch this error?
when does this happen?
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
wait so you made
identifier
in verification table unique?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
if a user already exist with the phone number, it should throw a proper error. I'll check if anything has changed.
im using update phonenumber
think it might be that
Any updates w this also have this issue
will be fixed on next release