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

Distinguish error types #317

Closed
tjconcept opened this issue Dec 5, 2014 · 4 comments
Closed

Distinguish error types #317

tjconcept opened this issue Dec 5, 2014 · 4 comments

Comments

@tjconcept
Copy link

If a user posts content with a bad content-type or otherwise invalid request, I would like to provide a decent error message back.
I'm currently doing this by wrapping code using Formidable in a try-clause, which works, but also prevents any other lower level error in reaching the surface and getting properly logged.

How should I distinguish these operational errors from others?
Maybe Formidable could sub class Error and expose it's error class for use in something like e instanceof formidable.Error? That would be nice to someone using bluebird.

@gabeio
Copy link
Contributor

gabeio commented Dec 10, 2014

As a side note: try-clauses are not good to wrap any production code in as node apparently runs slower (at least so I've heard)

@xarguments xarguments added this to the 1.3.1 milestone Jan 30, 2019
@GrosSacASac GrosSacASac removed this from the 1.3.1 milestone Feb 14, 2021
@GrosSacASac
Copy link
Contributor

GrosSacASac commented Feb 16, 2021

Have a look #686

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


const form = formidable({ multiples: true });

form.parse(req, (err, fields, files) => {
    if (err) {
        // check for a very specific error
        if (err.code === formidableErrors.maxFieldsExceeded) {

        }
        console.error(err);
        // nice error message and httpCode
        res.writeHead(err.httpCode || 400, { 'Content-Type': 'text/plain' });
        res.end(String(err));
        return;
    }
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify({ fields, files }, null, 2));
});

@aderchox
Copy link

aderchox commented Oct 9, 2022

This answered my question as well, I know this has been mentioned in the docs too where it says:

May have error.httpCode and error.code attached.

But in my opinion it's not clear enough as this example by you (@GrosSacASac ), so maybe adding it to the documentation too is not a bad idea.
Thank you.

@GrosSacASac
Copy link
Contributor

@aderchox added something in the readme e060e3f

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

No branches or pull requests

5 participants