Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Improve error messages in JSON parsing #167

Merged
merged 2 commits into from
Jan 12, 2016

Conversation

glittershark
Copy link
Contributor

Use Aeson's built-in with<type> functions, plus a custom error message
at the end of the ParamValP parser's Alternative chain to improve error
messages when parsing JSON requests.

Before:

❯ echo '{"cmd":"base:commands","params":{"plugin": "hare"}}' | hie --one-shot | jq
{
  "error": {
    "msg": "FromJSONError \"failed to parse field params: empty\"",
    "code": "ParseError",
    "info": null
  }
}

After:

❯ echo '{"cmd":"base:commands","params":{"plugin": "hare"}}' | stack exec -- hie --one-shot | jq
{
  "error": {
    "msg": "FromJSONError \"failed to parse field params: expected text, file, or position object, encountered String\"",
    "code": "ParseError",
    "info": null
  }
}

Closes #166

Use Aeson's built-in `with<type>` functions, plus a custom error message
at the end of the ParamValP parser's Alternative chain to improve error
messages when parsing JSON requests.

Before:
-------

```
❯ echo '{"cmd":"base:commands","params":{"plugin": "hare"}}' | hie
--one-shot | jq
{
  "error": {
    "msg": "FromJSONError \"failed to parse field params: empty\"",
    "code": "ParseError",
    "info": null

  }

}
```

After:
------

```
❯ echo '{"cmd":"base:commands","params":{"plugin": "hare"}}' | stack exec -- hie --one-shot | jq
{
  "error": {
    "msg": "FromJSONError \"failed to parse field params: expected text, file, or position object, encountered String\"",
    "code": "ParseError",
    "info": null
  }
}
```

Closes haskell#166
parseJSON _ = mzero
parseJSON = withObject "WireRequest" $ \v ->
WireReq <$>
v A..: "cmd" <*>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth not importing Aeson qualified here to make this consistent with the other files in the repo?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good.

Don't qualify Data.Aeson here, since it's not qualified anywhere else
@glittershark
Copy link
Contributor Author

@cocreature no longer qualifying Data.Aeson in that module

cocreature added a commit that referenced this pull request Jan 12, 2016
@cocreature cocreature merged commit 101949f into haskell:master Jan 12, 2016
@glittershark glittershark deleted the better-json-error-messages branch January 12, 2016 20:59
@alanz alanz added this to the prehistory milestone Feb 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants