Skip to content

Commit 9374054

Browse files
bugfix(@nestjs/common) File(s)Interceptor throws http status code 500
1 parent e7a3ad1 commit 9374054

5 files changed

Lines changed: 39 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.6.6
2+
### Bug Fixes:
3+
- **common**: `File(s)Interceptor` throws http status code 500 [#465](https://github.com/nestjs/nest/issues/437)
4+
15
## 4.6.5
26
### Bug Fixes
37
- **common**: `File(s)Interceptor` does not populate thrown exception [#437](https://github.com/nestjs/nest/issues/437)

src/common/interceptors/file.interceptor.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { NestInterceptor } from './../interfaces/nest-interceptor.interface';
33
import { Observable } from 'rxjs/Observable';
44
import { MulterOptions } from '../interfaces/external/multer-options.interface';
55
import { mixin } from '../decorators/core/component.decorator';
6+
import { transformException } from './multer/multer.utils';
67

78
export function FileInterceptor(fieldName: string, options?: MulterOptions) {
89
return mixin(
@@ -17,7 +18,8 @@ export function FileInterceptor(fieldName: string, options?: MulterOptions) {
1718
await new Promise((resolve, reject) =>
1819
this.upload.single(fieldName)(request, request.res, err => {
1920
if (err) {
20-
return reject(err);
21+
const error = transformException(err);
22+
return reject(error);
2123
}
2224
resolve();
2325
}),

src/common/interceptors/files.interceptor.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as multer from 'multer';
22
import { NestInterceptor } from './../interfaces/nest-interceptor.interface';
33
import { Observable } from 'rxjs/Observable';
44
import { MulterOptions } from '../interfaces/external/multer-options.interface';
5+
import { transformException } from './multer/multer.utils';
56

67
export function FilesInterceptor(fieldName: string, maxCount?: number, options?: MulterOptions) {
78
const Interceptor = class implements NestInterceptor {
@@ -15,7 +16,8 @@ export function FilesInterceptor(fieldName: string, maxCount?: number, options?:
1516
await new Promise((resolve, reject) =>
1617
this.upload.array(fieldName, maxCount)(request, request.res, err => {
1718
if (err) {
18-
return reject(err);
19+
const error = transformException(err);
20+
return reject(error);
1921
}
2022
resolve();
2123
}),
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const multerExceptions = {
2+
LIMIT_PART_COUNT: 'Too many parts',
3+
LIMIT_FILE_SIZE: 'File too large',
4+
LIMIT_FILE_COUNT: 'Too many files',
5+
LIMIT_FIELD_KEY: 'Field name too long',
6+
LIMIT_FIELD_VALUE: 'Field value too long',
7+
LIMIT_FIELD_COUNT: 'Too many fields',
8+
LIMIT_UNEXPECTED_FILE: 'Unexpected field',
9+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { InternalServerErrorException, HttpException, PayloadTooLargeException, BadRequestException } from './../../exceptions';
2+
import { multerExceptions } from './multer.constants';
3+
4+
export function transformException(error: Error | undefined) {
5+
if (!error || error instanceof HttpException) {
6+
return error;
7+
}
8+
switch (error.message) {
9+
case multerExceptions.LIMIT_FILE_SIZE:
10+
return new PayloadTooLargeException(error.message);
11+
case multerExceptions.LIMIT_FILE_COUNT:
12+
case multerExceptions.LIMIT_FIELD_KEY:
13+
case multerExceptions.LIMIT_FIELD_VALUE:
14+
case multerExceptions.LIMIT_FIELD_COUNT:
15+
case multerExceptions.LIMIT_UNEXPECTED_FILE:
16+
case multerExceptions.LIMIT_PART_COUNT:
17+
return new BadRequestException(error.message);
18+
}
19+
return error;
20+
}

0 commit comments

Comments
 (0)