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
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion 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,6 +23,9 @@ export function transformException(error: Error | undefined) {
case multerExceptions.LIMIT_UNEXPECTED_FILE:
case multerExceptions.LIMIT_PART_COUNT:
case multerExceptions.MISSING_FIELD_NAME:
if (error.field) {
return new BadRequestException(`${error.message} - ${error.field}`);
}
return new BadRequestException(error.message);
case busboyExceptions.MULTIPART_BOUNDARY_NOT_FOUND:
return new BadRequestException(error.message);
Expand Down
11 changes: 11 additions & 0 deletions packages/platform-express/test/multer/multer/multer.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,16 @@ describe('transformException', () => {
);
});
});
describe(`and has a 'field' property`, () => {
it('should return the field propery appended to the error message', () => {
const err = {
message: multerExceptions.LIMIT_UNEXPECTED_FILE,
field: 'foo',
};
expect(transformException(err as any).message).to.equal(
`${multerExceptions.LIMIT_UNEXPECTED_FILE} - foo`,
);
});
});
});
});