Coder Social home page Coder Social logo

Comments (9)

tunnckoCore avatar tunnckoCore commented on August 18, 2024

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

from formidable.

GrosSacASac avatar GrosSacASac commented on August 18, 2024

Where did you see that way of using this variable ?

from formidable.

FredTheNoob avatar FredTheNoob commented on August 18, 2024

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 😅

from formidable.

FredTheNoob avatar FredTheNoob commented on August 18, 2024

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

from formidable.

GrosSacASac avatar GrosSacASac commented on August 18, 2024
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)

from formidable.

FredTheNoob avatar FredTheNoob commented on August 18, 2024

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)

from formidable.

GrosSacASac avatar GrosSacASac commented on August 18, 2024

if (err instanceof FormidableErrors.default)

from formidable.

FredTheNoob avatar FredTheNoob commented on August 18, 2024

That doesn't work either

image

I have imported like so:

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

from formidable.

Corpra avatar Corpra commented on August 18, 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.

from formidable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.