graphql-upload: MaxFileSizeUploadError can't be caught and crashes the server

Hi!

I have a problem with a custom middleware and the use of the processRequest function. Here is my custom middleware:

try {
    return processRequest(request, options)
                    .then(body => {
                        console.log("body is : ", body);
                        ctx.request.body = body;
                        return next();
                    })
                    .catch(error => {
                        console.log("Error catched : ", error);
                        if (error.status && error.expose) response.status(error.status);
                        return next(error)
                    });
} catch (error) {
    console.log("Error catched bis : ", error);
}

If I try to upload a file bigger than the allowed limit, I get the following error and my app crash.

events.js:137
      throw er; // Unhandled 'error' event
      ^

MaxFileSizeUploadError: File truncated as it exceeds the size limit.
    at FileStream.file.stream.once (/Volumes/lake/projects/evibe/api-lovejs/node_modules/apollo-upload-server/lib/middleware.js:35:13)
    at Object.onceWrapper (events.js:255:19)
    at FileStream.emit (events.js:160:13)

I can’t capture the error. Any idea?

Thanks 😃

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 16 (8 by maintainers)

Commits related to this issue

Most upvoted comments

I have the same problem, in my case file stream throw error and not exist handlers for that error.

node-backend            |   PayloadTooLargeError: File truncated as it exceeds the 3145728 byte size limit.
node-backend            |       at FileStream.<anonymous> (/app/node_modules/graphql-upload/public/processRequest.js:289:21)
node-backend            |       at FileStream.emit (node:events:390:28)
node-backend            |       at PartStream.onData (/app/node_modules/busboy/lib/types/multipart.js:221:18)
node-backend            |       at PartStream.emit (node:events:390:28)
node-backend            |       at addChunk (node:internal/streams/readable:315:12)
node-backend            |       at readableAddChunk (node:internal/streams/readable:289:9)
node-backend            |       at PartStream.Readable.push (node:internal/streams/readable:228:10)
node-backend            |       at Dicer._oninfo (/app/node_modules/dicer/lib/Dicer.js:190:36)
node-backend            |       at SBMH.<anonymous> (/app/node_modules/dicer/lib/Dicer.js:126:10)
node-backend            |       at SBMH.emit (node:events:390:28) {
node-backend            |     locations: [ [Object] ],
node-backend            |     path: [ 'createProduct' ]
node-backend            |   }

Solution in my case: wrap stream in promise

export const resolvers = {
  // ... other resolvers ...
  Mutation: {
    uploadImageResolver: async (_, { image }) => {
      // throwing error
      await new Promise(async (res, rej) => {
        const { createReadStream } = await image;
        const stream = createReadStream();
        stream.on("error", (err) => {
            rej(err);
        });

        // ... some code about using that stream, like save file ...
        // await saveFile(stream);
        res(); // success
      });
    },
  },
};

@jaydenseric I use apollo-server-koa, how can i fix this? wait apollo-server-koa use new version of apollo-upload-server ?