Browse Source

fix: handle body parser error and avoid logging

pull/8200/head
mertmit 8 months ago
parent
commit
8b1e26277c
  1. 1
      packages/nocodb-sdk/src/lib/globals.ts
  2. 9
      packages/nocodb/src/filters/global-exception/global-exception.filter.ts
  3. 4
      packages/nocodb/src/helpers/catchError.ts

1
packages/nocodb-sdk/src/lib/globals.ts

@ -144,6 +144,7 @@ export enum NcErrorType {
INTERNAL_SERVER_ERROR = 'INTERNAL_SERVER_ERROR', INTERNAL_SERVER_ERROR = 'INTERNAL_SERVER_ERROR',
DATABASE_ERROR = 'DATABASE_ERROR', DATABASE_ERROR = 'DATABASE_ERROR',
UNKNOWN_ERROR = 'UNKNOWN_ERROR', UNKNOWN_ERROR = 'UNKNOWN_ERROR',
BAD_JSON = 'BAD_JSON',
} }
type Roles = OrgUserRoles | ProjectRoles | WorkspaceUserRoles; type Roles = OrgUserRoles | ProjectRoles | WorkspaceUserRoles;

9
packages/nocodb/src/filters/global-exception/global-exception.filter.ts

@ -28,6 +28,15 @@ export class GlobalExceptionFilter implements ExceptionFilter {
const response = ctx.getResponse<Response>(); const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>(); const request = ctx.getRequest<Request>();
// catch body-parser error and replace with NcBaseErrorv2
if (
exception.name === 'BadRequestException' &&
exception.status === 400 &&
/^Unexpected token .*? in JSON/.test(exception.message)
) {
exception = new NcBaseErrorv2(NcErrorType.BAD_JSON);
}
// skip unnecessary error logging // skip unnecessary error logging
if ( if (
process.env.NC_ENABLE_ALL_API_ERROR_LOGGING === 'true' || process.env.NC_ENABLE_ALL_API_ERROR_LOGGING === 'true' ||

4
packages/nocodb/src/helpers/catchError.ts

@ -514,6 +514,10 @@ const errorHelpers: {
message: (feature: string) => `${feature} is not implemented`, message: (feature: string) => `${feature} is not implemented`,
code: 501, code: 501,
}, },
[NcErrorType.BAD_JSON]: {
message: 'Invalid JSON in request body',
code: 400,
},
}; };
function generateError( function generateError(

Loading…
Cancel
Save