Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multer error fieldname #13407

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
refactor(platform-express): modify error arg type
Add the possible `field` property to the `error` arg in
`transformExecption`.
  • Loading branch information
MegaSpaceHamlet committed Apr 8, 2024
commit ef12d355ff91377a2fd1d5a66afe60841d4be6ec
8 changes: 5 additions & 3 deletions packages/platform-express/multer/multer/multer.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
} from '@nestjs/common';
import { multerExceptions, busboyExceptions } from './multer.constants';

export function transformException(error: Error | undefined) {
// Multer may add in a 'field' property to the error
// https://github.com/expressjs/multer/blob/aa42bea6ac7d0cb8fcb279b15a7278cda805dc63/lib/multer-error.js#L19
export function transformException(
error: (Error & { field?: string }) | undefined,
) {
if (!error || error instanceof HttpException) {
return error;
}
Expand All @@ -19,9 +23,7 @@ export function transformException(error: Error | undefined) {
case multerExceptions.LIMIT_UNEXPECTED_FILE:
case multerExceptions.LIMIT_PART_COUNT:
case multerExceptions.MISSING_FIELD_NAME:
//@ts-expect-error: Multer may attach the fieldname to the error object
if (error.field) {
//@ts-expect-error: Multer may attach the fieldname to the error object
return new BadRequestException(`${error.message} - ${error.field}`);
}
return new BadRequestException(error.message);
Expand Down