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
Give a clearer error message when payload doesn't match with provided content-type #2281
Comments
it looks like JSON parser used in your example does not raise grape/lib/grape/parser/json.rb Lines 8 to 11 in edc6abe
could you ensure that the parser raises the error mentioned above? |
Thanks @dm1try. Oj.load('d:', mode: :object)
# => Oj::ParseError (unexpected character (after ) at line 1, column 1 [parse.c:804])
Oj.load('d:', mode: :compat)
# => EncodingError (Empty input (after ) at line 1, column 1 [parse.c:1116] in 'd:) Could we define Pseudo code: module Grape
if Object.const_defined? :MultiJson
Json = ::MultiJson
+ JsonParseError = Json::ParseError
else
Json = ::JSON
- Json::ParseError = Json::ParserError
+ JsonParseError = Json::ParserError
end
end module Grape
module Parser
module Json
class << self
def call(object, _env)
::Grape::Json.load(object)
- rescue ::Grape::Json::ParseError
+ rescue ::Grape::JsonParseError
# handle JSON parsing errors via the rescue handlers or provide error message
raise Grape::Exceptions::InvalidMessageBody.new('application/json')
end
end
end
end
end For someone who may want to replace the parser with module Grape
Json = ::Oj
JsonParseError = ::EncodingError
end |
Moving
WDYT? |
Documentation and tests, I like it! @dblock agreed, the issue is about how to swap JSON parser and do not break anything(btw, the example you provided is about formatter but not a parser). In the issue above a new parser was set implicitly by assigning class OjCompactParser
def self.call(object, _env)
Oj.load(object, mode: :compat)
rescue EncodingError
raise Grape::Exceptions::InvalidMessageBody.new('application/json')
end
end
class API < Grape::API
parser :json, OjCompactParser
...
end @khiav223577 I'm not sure how class OjCompact
def self.load(object)
Oj.load(object, mode: :compat)
rescue EncodingError
raise Oj::ParseError
end
end
Grape::Json = OjCompact But I would stick with a public interface in the first example(you should try if it works :). |
😎 |
@dm1try thanks for your reply. The first example looks great, I'll try and see if it works or not :) |
@khiav223577 Care to contribute to the README? |
I'm willing to :) |
Hi @dm1try, Since there are many other places besides formatter that use module CustomGrapeParser
ParseError = ::EncodingError
class << self
def load(object)
Oj.load(object, mode: :compat)
end
def dump(object)
Oj.dump(object, mode: :compat)
end
end
end
Grape::Json = CustomGrapeParser |
In that case, I think your example is the most suitable solution. Thanks! |
Let we are not support
application/json
content-type.When user send a request with wrong content-type, the response message is clear:
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:
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:
grape/lib/grape/middleware/formatter.rb
Lines 115 to 116 in f5d9831
grape/lib/grape/middleware/error.rb
Lines 38 to 40 in f5d9831
The text was updated successfully, but these errors were encountered: