核销代码
This commit is contained in:
@@ -5,6 +5,23 @@ import {
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
|
||||
function prismaErrorMessage(exception: Prisma.PrismaClientKnownRequestError): string {
|
||||
switch (exception.code) {
|
||||
case 'P2002':
|
||||
return '数据冲突,请刷新后重试';
|
||||
case 'P2003':
|
||||
return '关联数据不存在,请检查门店/权益配置';
|
||||
case 'P2021':
|
||||
case 'P2022':
|
||||
return '数据库表结构未同步,请在服务器执行 prisma db push';
|
||||
case 'P2025':
|
||||
return '记录不存在或已被删除';
|
||||
default:
|
||||
return exception.message;
|
||||
}
|
||||
}
|
||||
|
||||
@Catch()
|
||||
export class HttpExceptionFilter implements ExceptionFilter {
|
||||
@@ -27,10 +44,29 @@ export class HttpExceptionFilter implements ExceptionFilter {
|
||||
return;
|
||||
}
|
||||
|
||||
if (exception instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
console.error(exception);
|
||||
response.status(HttpStatus.BAD_REQUEST).json({
|
||||
code: 400,
|
||||
message: prismaErrorMessage(exception),
|
||||
data: null,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (exception instanceof SyntaxError && /BigInt/i.test(exception.message)) {
|
||||
response.status(HttpStatus.BAD_REQUEST).json({
|
||||
code: 400,
|
||||
message: 'ID 格式无效',
|
||||
data: null,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
console.error(exception);
|
||||
response.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
|
||||
code: 500,
|
||||
message: 'Internal server error',
|
||||
message: exception instanceof Error ? exception.message : 'Internal server error',
|
||||
data: null,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user