wobsoriano / h3-formidable

File upload parsing utility for h3 and Nuxt.
MIT License
61 stars 3 forks source link

How to get the error #10

Closed prpanto closed 1 year ago

prpanto commented 1 year ago

I dont know how to get the error for example I set a size limit and I dont know how to get the error...

export default defineEventHandler(async (event) => {
  const { files, fields } = await readFiles(event, {
    includeFields: true,
    maxFileSize: 1024 * 1024 * 1,
  });
});

I expecting something like:

export default defineEventHandler(async (event) => {
  const { files, fields, errors } = await readFiles(event, {
    includeFields: true,
    maxFileSize: 1024 * 1024 * 1,
  });
});
prpanto commented 1 year ago

I try to catch the error with try/catch and have an error...

 ERROR  [h3] [unhandled] write EPIPE                                                                                                                                               

  at WriteWrap.onWriteComplete [as oncomplete] (node:internal/stream_base_commons:94:16)
  try {
    const { files, fields } = await readFiles(event, {
      includeFields: true,
      maxFiles: 1,
      maxFileSize: fileSize,
    });
  } catch (error) {
    if (error.code === formidableErrors.maxFilesExceeded) {
      throw createError({
        statusCode: 422,
        message: 'Can\'t upload more than one image.'
      });
    }

    if (error.code === formidableErrors.biggerThanTotalMaxFileSize) {
      throw createError({
        statusCode: 422,
        message: `The file(s) is larger than ${(fileSize / (1024 * 1024))}MB.`
      });
    }
  }
wobsoriano commented 1 year ago

Hi! Please upgrade to v1 and check if you get the expected error.

There is a breaking change - https://github.com/wobsoriano/h3-formidable/pull/11

wobsoriano commented 1 year ago

Feel free to reopen!