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

fix(express): Map missing multer error and map busboy errors #10880

Merged
merged 1 commit into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/platform-express/multer/multer/multer.constants.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
export const multerExceptions = {
// from https://github.com/expressjs/multer/blob/master/lib/multer-error.js
LIMIT_PART_COUNT: 'Too many parts',
LIMIT_FILE_SIZE: 'File too large',
LIMIT_FILE_COUNT: 'Too many files',
LIMIT_FIELD_KEY: 'Field name too long',
LIMIT_FIELD_VALUE: 'Field value too long',
LIMIT_FIELD_COUNT: 'Too many fields',
LIMIT_UNEXPECTED_FILE: 'Unexpected field',
MISSING_FIELD_NAME: 'Field name missing',
};

export const busboyExceptions = {
// from https://github.com/mscdex/busboy/blob/master/lib/types/multipart.js
MULTIPART_BOUNDARY_NOT_FOUND: 'Multipart: Boundary not found',
MULTIPART_MALFORMED_PART_HEADER: 'Malformed part header',
MULTIPART_UNEXPECTED_END_OF_FORM: 'Unexpected end of form',
MULTIPART_UNEXPECTED_END_OF_FILE: 'Unexpected end of file',
};
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 @@ -3,7 +3,7 @@ import {
HttpException,
PayloadTooLargeException,
} from '@nestjs/common';
import { multerExceptions } from './multer.constants';
import { multerExceptions, busboyExceptions } from './multer.constants';

export function transformException(error: Error | undefined) {
if (!error || error instanceof HttpException) {
Expand All @@ -18,7 +18,14 @@ export function transformException(error: Error | undefined) {
case multerExceptions.LIMIT_FIELD_COUNT:
case multerExceptions.LIMIT_UNEXPECTED_FILE:
case multerExceptions.LIMIT_PART_COUNT:
case multerExceptions.MISSING_FIELD_NAME:
return new BadRequestException(error.message);
case busboyExceptions.MULTIPART_BOUNDARY_NOT_FOUND:
return new BadRequestException(error.message);
case busboyExceptions.MULTIPART_MALFORMED_PART_HEADER:
case busboyExceptions.MULTIPART_UNEXPECTED_END_OF_FORM:
case busboyExceptions.MULTIPART_UNEXPECTED_END_OF_FILE:
return new BadRequestException(`Multipart: ${error.message}`);
}
return error;
}
26 changes: 23 additions & 3 deletions packages/platform-express/test/multer/multer/multer.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import {
PayloadTooLargeException,
} from '@nestjs/common';
import { expect } from 'chai';
import { multerExceptions } from '../../../multer/multer/multer.constants';
import {
multerExceptions,
busboyExceptions,
} from '../../../multer/multer/multer.constants';
import { transformException } from '../../../multer/multer/multer.utils';

describe('transformException', () => {
describe('if error does not exist', () => {
it('behave as identity', () => {
it('should behave as identity', () => {
const err = undefined;
expect(transformException(err)).to.be.eq(err);
});
});
describe('if error is instance of HttpException', () => {
it('behave as identity', () => {
it('should behave as identity', () => {
const err = new HttpException('response', 500);
expect(transformException(err)).to.be.eq(err);
});
Expand All @@ -37,5 +40,22 @@ describe('transformException', () => {
);
});
});
describe('and is busboy/multipart exception', () => {
it('should return "BadRequestException"', () => {
const err = { message: busboyExceptions.MULTIPART_BOUNDARY_NOT_FOUND };
expect(transformException(err as any)).to.be.instanceof(
BadRequestException,
);
});

it('should return "BadRequestException"', () => {
const err = {
message: busboyExceptions.MULTIPART_UNEXPECTED_END_OF_FORM,
};
expect(transformException(err as any)).to.be.instanceof(
BadRequestException,
);
});
});
});
});