Skip to content

Better error for empty stringΒ #3

Open
@rdmurphy

Description

@rdmurphy

Hello! πŸ‘‹

Have been on the hunt for better ways to return feedback with JSON.parse, and was thrilled to find this. Recently I hit a new-to-me error with JSON.parse β€” you cannot do a parse against an empty string. It'll throw the following:

Uncaught SyntaxError: Unexpected end of JSON input

With json-parse-better-errors it is better, but still a little cryptic:

SyntaxError: Unexpected end of JSON input while parsing near ''

Neither gives much feedback in terms of "hey, you just tried to pass me nothing." Much like the catch for the passing of a non-string or Array, do you think something like this would be within the scope of json-parse-better-errors?

I'm not 100% sure what would be the best way to determine when something is truly an empty string, though. Maybe a combination of String.prototype.trim() and a string.length check?

Thanks for the useful library!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions