node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeScript - Unable to access FormidableError on formidable.errors

FredTheNoob opened this issue · comments

Support plan

  • Which support plan is this issue covered by? (Community, Sponsor, Enterprise): Community
  • Currently blocking your project/work? (yes/no): no
  • Affecting a production system? (yes/no): no

Context

  • Node.js version: 18.18.2
  • Release Line of Formidable (Legacy, Current, Next): Current
  • Formidable exact version: ^3.4.5
  • Environment (node, browser, native, OS): node
  • Used with (popular names of modules): express, typescript, es6 imports

What are you trying to achieve or the steps to reproduce?

I'd like to access the FormidableError object when I'm throwing an error, this is my use case:

router.post("/user/upload", validateAuthToken, async (req, res, next) => {
    const form = formidable({
        maxFiles: 1, 
        keepExtensions: true, 
        maxFileSize: MAX_FILE_SIZE, 
        allowEmptyFiles: false,
    });

    form.parse(req, async (err, fields, files) => {
        if (err) {
            if (err instanceof formidable.errors.FormidableError) {
                return res.status(err.httpCode ? err.httpCode : 400).send(err.message);
            }
        }
    })
});

In postman I'm passing multipart formdata, where I purposely input a file above the MAX_FILE_SIZE, the error is thrown properly, but I just seem unable to get the FormidableError object.

What was the result you got?

TypeError: Cannot read properties of undefined (reading 'FormidableError')

What result did you expect?

That the above use case works

That's because errors export named variables with "error codes", not directly the error class.

That's a good point, we'll fix it. I think you can access the class on errors.default.

image

Where did you see that way of using this variable ?