Skip to content

Commit

Permalink
Merge pull request #10880 from max-mathieu/multer-errors
Browse files Browse the repository at this point in the history
fix(express): Map missing multer error and map busboy errors
  • Loading branch information
kamilmysliwiec committed Feb 1, 2023
2 parents fbf0297 + 4c38a76 commit 26c7e3d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
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,
);
});
});
});
});

0 comments on commit 26c7e3d

Please sign in to comment.