nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.31k stars 7.59k forks source link

FilesInterceptor return UnhandledPromiseRejection when number of fields exceed maxCount #3413

Closed ivashog closed 4 years ago

ivashog commented 4 years ago

Bug Report

Current behavior

FilesInterceptor return UnhandledPromiseRejection when number of files exceed maxCount parameter. Error with stack trace:

UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type object
    at validChunk (_stream_writable.js:265:10)
    at WriteStream.Writable.write (_stream_writable.js:299:21)
    at Function.printStackTrace (/www/RNBO/rnbo-oda-api/node_modules/@nestjs/common/services/logger.service.js:116:24)
    at Function.error (/www/RNBO/rnbo-oda-api/node_modules/@nestjs/common/services/logger.service.js:58:14)
    at HttpExceptionFilter.catch (/www/RNBO/rnbo-oda-api/src/app/_common/http-error.filter.ts:29:20)
    at ExceptionsHandler.invokeCustomFilters (/www/RNBO/rnbo-oda-api/node_modules/@nestjs/core/exceptions/exceptions-handler.js:31:26)
    at ExceptionsHandler.next (/www/RNBO/rnbo-oda-api/node_modules/@nestjs/core/exceptions/exceptions-handler.js:12:18)
    at /www/RNBO/rnbo-oda-api/node_modules/@nestjs/core/router/router-proxy.js:12:35
    at processTicksAndRejections (internal/process/task_queues.js:88:5)

Code:

@Post('upload')
@UseInterceptors(FilesInterceptor(config.csv.formFieldName, config.csv.maxFilesCount, csvMulterOptions))
uploadCsvFiles(@UploadedFiles() csvFiles: MulterFile[]) {
     // ...
}

Error disappears when i add to nest/packages/platform-express/multer/interceptors/files.interceptor.ts this line

await new Promise((resolve, reject) =>
        this.multer.array(fieldName, maxCount)(
          ctx.getRequest(),
          ctx.getResponse(),
          (err: any) => {
            if (err) {
              console.log(''); // it's solve problem
              const error = transformException(err);
              return reject(error);
            }
            resolve();
          },
        ),
      );

Here is repository with reproducing the bug https://github.com/ivashog/nest-multer-bug

Expected behavior

Nestjs must handle error and return exception with LIMIT_FILE_COUNT MulterError

Environment


Nest version: 6.0.0


For Tooling issues:
- Node version: v12.0.0  
- Platform: Linux Mint 19.1 Tessa  

Others:

kamilmysliwiec commented 4 years ago

Please, provide a minimal repository which reproduces your issue.

ivashog commented 4 years ago

@kamilmysliwiec, I created repository with bug from my project. See Reproducing bug section in repo README

ivashog commented 4 years ago

I am sorry! This is my carelessness... I carefully studied error stack trace and find real error source - this is my own HttpExceptionFilter where i logging errors and passed object as trace parameter to Logger.error method! That was stupid enough... closes #3413

ivashog commented 4 years ago

But maybe it makes sense add a some type check for trace argument in error method inside nest/packages/common/services/logger.service.ts?

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.