Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript - Unable to access FormidableError on formidable.errors #972

Closed
FredTheNoob opened this issue Mar 8, 2024 · 10 comments
Closed
Labels

Comments

@FredTheNoob
Copy link

FredTheNoob commented Mar 8, 2024

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

@FredTheNoob FredTheNoob added the bug label Mar 8, 2024
@FredTheNoob FredTheNoob changed the title Unable to access FormidableError on formidable.errors TypeScript - Unable to access FormidableError on formidable.errors Mar 8, 2024
@tunnckoCore
Copy link
Member

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

@GrosSacASac
Copy link
Contributor

Where did you see that way of using this variable ?

@FredTheNoob
Copy link
Author

Where did you see that way of using this variable ?

Nowhere, in retrospect I don't know if this is even the right way to do it at all 😅

@FredTheNoob
Copy link
Author

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

That doesn't seem to work 😔

image

@GrosSacASac
Copy link
Contributor

import formidable, {errors as FormidableErrors} from 'formidable';

console.log(FormidableErrors.default);

formidable.errors is not the same as FormidableErrors.
The way you import is critical here

https://github.com/node-formidable/formidable/blob/master/src/FormidableError.js

or

As you can see all errors have a httpCode

So you could do the following

if (err.httpCode !== undefined)

@FredTheNoob
Copy link
Author

I don't know how to apply that to my example @GrosSacASac.
I've tried this:

import formidable, {errors as FormidableErrors} from 'formidable';

....

form.parse(req, async (err, fields, files) => {
        if (err) {
            if (err instanceof FormidableErrors.FormidableError) {
                if (err.httpCode !== undefined) {
                    return res.status(err.httpCode).send(err.message);
                }
            }
        }
});

But get this error:

file:///root/easyrate/easyrate-backend/js/routes/user.js:283
            if (err instanceof FormidableErrors.FormidableError) {
                    ^

TypeError: Right-hand side of 'instanceof' is not an object
    at file:///root/easyrate/easyrate-backend/js/routes/user.js:283:21
    at zalgoSafe (/root/easyrate/easyrate-backend/node_modules/dezalgo/dezalgo.js:20:10)
    at f (/root/easyrate/easyrate-backend/node_modules/once/once.js:25:25)
    at IncomingForm.<anonymous> (file:///root/easyrate/easyrate-backend/node_modules/formidable/src/Formidable.js:225:7)
    at IncomingForm.emit (node:events:519:28)
    at IncomingForm._error (file:///root/easyrate/easyrate-backend/node_modules/formidable/src/Formidable.js:495:10)
    at Stream.<anonymous> (file:///root/easyrate/easyrate-backend/node_modules/formidable/src/Formidable.js:385:14)
    at Stream.emit (node:events:519:28)
    at MultipartParser.dataPropagation (file:///root/easyrate/easyrate-backend/node_modules/formidable/src/plugins/multipart.js:103:22)
    at MultipartParser.emit (node:events:531:35)

Node.js v21.7.1
import formidable, {errors as FormidableErrors} from 'formidable';

console.log(FormidableErrors.default);

formidable.errors is not the same as FormidableErrors. The way you import is critical here

https://github.com/node-formidable/formidable/blob/master/src/FormidableError.js

or

As you can see all errors have a httpCode

So you could do the following

if (err.httpCode !== undefined)

@GrosSacASac
Copy link
Contributor

GrosSacASac commented Apr 6, 2024

if (err instanceof FormidableErrors.default)

@FredTheNoob
Copy link
Author

That doesn't work either

image

I have imported like so:

import formidable, {errors as FormidableErrors} from 'formidable';

@Corpra
Copy link

Corpra commented Apr 26, 2024

@FredTheNoob

I'm doing this successfully with the following:

import { errors as formidableErrors } from 'formidable';

// ...

if e (instanceof formidableErrors.FormidableError)

But I'm on v2 of formidable, not sure if that matters.

@vircoding
Copy link

I'm doing this to verify the error type in v3.5.2

import formidable, { errors as formidableErrors } from 'formidable';

try {
  [fields, files] = await form.parse(event.node.req);
} catch (error) {
  if (error && typeof error === 'object' && 'code' in error) {
    if (error.code === formidableErrors.biggerThanMaxFileSize)
      throw new BadRequestError('File exceeds the maximum allowed size of 5MB');
    }
  throw new UnexpectedError();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants