ruby-grape / ruby-grape/grape

Give a clearer error message when payload doesn't match with provided content-type

Open
#2,281 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

Let we are not support application/json content-type.
When user send a request with wrong content-type, the response message is clear:

{
  "error" => "The provided content-type 'application/json' is not supported"
}

Let we support application/json content-type.
When user send a request with correct content-type, but with wrong payload (the format of it is not json),
the response message will be strange:

{
  "error" => "Empty input (after ) at line 1, column 1 [parse.c:1072] in '......"
}

It will be nice if we can provide clearer error message.
But it seems to be impossible to add custom messages without overriding codes in the gem.

The error is throwed and rescued inside:

https://github.com/ruby-grape/grape/blob/f5d9831bac2e2dd439d0f3901797995a91139690/lib/grape/middleware/formatter.rb#L115-L116
https://github.com/ruby-grape/grape/blob/f5d9831bac2e2dd439d0f3901797995a91139690/lib/grape/middleware/error.rb#L38-L40

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/grape/middleware/formatter.rb around lines 115-116 and lib/grape/middleware/error.rb around lines 38-40, then reproduce a request using application/json with an invalid JSON payload. Trace the exception through formatting and rescue handling to determine where a clearer response can be introduced. Done means malformed JSON produces a clear error message instead of the parser's raw message.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.