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 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
Next Next commit
feat(platform-express): multer errors show 'field'
Multer errors may include a 'field' property for certain errors. Pass it
along into the error message.
  • Loading branch information
MegaSpaceHamlet committed Mar 31, 2024
commit 50e1d705c3d4285675ca5f56e35dc99cd2239910
5 changes: 5 additions & 0 deletions packages/platform-express/multer/multer/multer.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ 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);
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`,
);
});
});
});
});