Fix error handle
fix pagination issue change logic for change passsword and profile update
This commit is contained in:
@@ -77,43 +77,6 @@ export const auth = betterAuth({
|
||||
});
|
||||
},
|
||||
},
|
||||
update: {
|
||||
before: async (user, ctx) => {
|
||||
if (ctx?.context.session && ctx?.path === '/update-user') {
|
||||
const newUser = JSON.parse(JSON.stringify(user));
|
||||
const keys = Object.keys(newUser);
|
||||
const oldUser = Object.fromEntries(
|
||||
Object.entries(ctx?.context.session?.user).filter(([key]) =>
|
||||
keys.includes(key),
|
||||
),
|
||||
);
|
||||
await createAuditLog({
|
||||
action: LOG_ACTION.UPDATE,
|
||||
tableName: DB_TABLE.USER,
|
||||
recordId: ctx?.context.session?.user.id,
|
||||
oldValue: JSON.stringify(oldUser),
|
||||
newValue: JSON.stringify(newUser),
|
||||
userId: ctx?.context.session?.user.id,
|
||||
});
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
account: {
|
||||
update: {
|
||||
after: async (account, context) => {
|
||||
if (context?.path === '/change-password') {
|
||||
await createAuditLog({
|
||||
action: LOG_ACTION.CHANGE_PASSWORD,
|
||||
tableName: DB_TABLE.ACCOUNT,
|
||||
recordId: account.id,
|
||||
oldValue: 'Change Password',
|
||||
newValue: 'Change Password',
|
||||
userId: account.userId,
|
||||
});
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
session: {
|
||||
create: {
|
||||
|
||||
1
src/lib/errors/index.ts
Normal file
1
src/lib/errors/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './parse-error';
|
||||
77
src/lib/errors/parse-error.ts
Normal file
77
src/lib/errors/parse-error.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { Prisma } from '@/generated/prisma/client';
|
||||
|
||||
export type ErrorBody = {
|
||||
message?: unknown;
|
||||
code?: unknown;
|
||||
status?: unknown;
|
||||
};
|
||||
|
||||
function hasErrorBody(error: unknown): error is { body: ErrorBody } {
|
||||
return (
|
||||
typeof error === 'object' &&
|
||||
error !== null &&
|
||||
'body' in error &&
|
||||
typeof (error as any).body === 'object'
|
||||
);
|
||||
}
|
||||
|
||||
export function parseError(error: unknown): {
|
||||
message: string;
|
||||
code?: string;
|
||||
status?: number;
|
||||
} {
|
||||
// Recognize AppError even if it's a plain object from network
|
||||
if (
|
||||
typeof error === 'object' &&
|
||||
error !== null &&
|
||||
'name' in error &&
|
||||
(error as any).name === 'AppError' &&
|
||||
'code' in error &&
|
||||
'message' in error &&
|
||||
'status' in error
|
||||
) {
|
||||
return {
|
||||
message: (error as any).message,
|
||||
code: (error as any).code,
|
||||
status: (error as any).status,
|
||||
};
|
||||
}
|
||||
|
||||
// better-auth / fetch error (có body)
|
||||
if (hasErrorBody(error)) {
|
||||
return {
|
||||
message:
|
||||
typeof error.body.message === 'string'
|
||||
? error.body.message
|
||||
: 'Unknown error',
|
||||
code: typeof error.body.code === 'string' ? error.body.code : undefined,
|
||||
status:
|
||||
typeof error.body.status === 'number' ? error.body.status : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
// Prisma (giữ nguyên code như "P2002")
|
||||
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
return {
|
||||
message: error.message,
|
||||
code: error.code,
|
||||
status: 400,
|
||||
};
|
||||
}
|
||||
|
||||
// Error thường
|
||||
if (error instanceof Error) {
|
||||
return {
|
||||
message: error.message,
|
||||
code: 'NORMAL_ERROR',
|
||||
status: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
// Fallback
|
||||
return {
|
||||
message: String(error),
|
||||
code: 'NORMAL_ERROR',
|
||||
status: undefined,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user