Skip to content

JSON.parse can be dangerous #84

@Jbarget

Description

@Jbarget

https://github.com/fac-12/BudgetPlanner/blob/master/public/index.js#L22

If the xhr.responseText is a string here, JSON.parse will fail with Uncaught SyntaxError: Unexpected token a in JSON at position 0, which will break things.

So its good to 'wrap' JSON.parse in a try/catch block, see below for an example:
https://stackoverflow.com/questions/29797946/handling-bad-json-parse-in-node-safely

PS. please share this with the whole cohort as its something thats valuable to know

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions