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

Ambiguous error when parsing some of falsy values #16

Closed
Hoishin opened this issue Feb 14, 2018 · 10 comments
Closed

Ambiguous error when parsing some of falsy values #16

Hoishin opened this issue Feb 14, 2018 · 10 comments

Comments

@Hoishin
Copy link

Hoishin commented Feb 14, 2018

When I parsed undefined, the error was

JSONError: Cannot read property 'length' of undefined
    at module.exports ((...)node_modules/parse-json/index.js:26:19)

For NaN

JSONError: txt.slice is not a function

It would be more friendly error if it just says something like don't give me undefined/NaN! (in more formal way, of course)

I don't know if this has mentioned before, but at least I could not find related issues.

@Hoishin
Copy link
Author

Hoishin commented Feb 19, 2018

I opened PR on json-parse-better-errors, but I haven't got response for 5 days even though the owner seems to be active.
How long should I wait before opening PR in this repo?

(Sorry if this is a weird question, by the way. I am new to open source contribution.)

@sindresorhus
Copy link
Owner

I opened PR on json-parse-better-errors, but I haven't got response for 5 days even though the owner seems to be active.

Maintainers are often busy with lots of repos and can't always prioritize reviewing PRs. Give it some time. To maximize your chances of getting it merged, try to make the minimal amount of changes to fix the problem, and don't forget to add tests ;)

I would for example, throw early, so you don't have to wrap the whole existing code in an else statement:

if (typeof txt !== 'string') {
  const type = typeof txt === 'undefined' ? ' ' : ` the ${typeof txt} `
  e.message = `Cannot parse${type}${String(txt)}`
  throw e;
}

I'm also not sure why you're special-casing undefined.

@Hoishin
Copy link
Author

Hoishin commented Feb 19, 2018

Thanks for the advice
I think I will add some tests meanwhile. And yes throwing early surely is better, I don't know why I didn't do that xD

As for special-casing undefined, I thought it would be informative to throw with messages like Cannot parse the number Infinity because it is an error due to an invalid type. But it doesn't look good to say Cannot parse the undefined undefined, so I just made it say Cannot parse undefined.

@sindresorhus
Copy link
Owner

sindresorhus commented Feb 19, 2018

Cannot parse the number Infinity

I don't see the point in mentioning that Infinity is a number. I would go with:

Cannot parse Infinity

@sindresorhus
Copy link
Owner

Actually, why even do the check after parsing. We know up front those types are not valid, so you can just throw a TypeError on line 5.

@Hoishin
Copy link
Author

Hoishin commented Feb 19, 2018

I agree that Infinity is clearly number and doesn't need its type mentioned.
After a while of exploration in JavaScript world, only convincing reason to mention the type I've found is an empty array [], which would be an error with the message Cannot parse . Probably not worth mentioning types then.

The reason I put the check after parsing is to preserve performance for valid strings. Would it be still better to always check the type before parsing?

@sindresorhus
Copy link
Owner

only convincing reason to mention the type I've found is an empty array [], which would be an error with the message Cannot parse. Probably not worth mentioning types then.

I would rather just special-case this one, with:

Cannot stringify empty array


The reason I put the check after parsing is to preserve performance for valid strings. Would it be still better to always check the type before parsing?

That's a good point. Yeah, makes more sense to optimize for the common-case.

@Hoishin
Copy link
Author

Hoishin commented Feb 19, 2018

Yeah the special-case message looks good. Thank you so much for the help.

@Hoishin
Copy link
Author

Hoishin commented Mar 31, 2018

@sindresorhus The fix is now merged to json-parse-better-errors and released as v1.0.2. Since it's patch version it should automatically be fixed for this package.

@sindresorhus
Copy link
Owner

Great! Thanks for working on it :)

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

No branches or pull requests

2 participants