cowboy
cowboy copied to clipboard
no match of right hand side value: 2
Was facing this issue in Pleroma pleroma/pleroma#2029 where all 204 responses suddenly started returning 500 on the latest Cowboy.
Basically, this code in Phoenix, with the default Jason encoder, no longer works:
conn
|> put_status(204)
|> json("")
Turns out it's because of this:
1> Jason.encode! ""
"\"\""
json/2
in Phoenix encodes ""
with Jason to "\"\""
which it passes in the body. Therefore the body contains a 2 characters, which isn't 0, which causes the match in Cowboy reply/4
to fail on the 204 response, which causes all Pleroma controllers which previously returned 204 to now return 500.
This issue is arguably caused by everything except Cowboy, and I went ahead and patched it in Pleroma, but I wanted to raise it here anyway, because it was a pretty hard problem to solve as an Elixir newbie, and I think it could use a more informative error. Thanks!
Yeah.
We've just debugged the same issue and it took us quite a while to figure out why the error occurs.
It's quite confusing to only get a MatchError
with no further information.
As such we strongly support @alexgleason proposal to improve upon the error message. 🙂
Just ran into this one, indeed the error message is very confusing. no match of right hand side value: 340
Within the context of phoenix it's also unfortunate controller tests won't show this error. This isn't an issue for cowboy to resolve, just making a note of it here.
Still getting this error. So it is forcing you to return empty
json on 204, but the error message is not helping at all.
So in my case changing the following helped.
conn = conn |> put_status(:no_content)
- json(conn, nil)
+ send_resp(conn, 204, "")
I will make a better error message.
The better error message will be in 2.11:
=ERROR REPORT==== 9-Jan-2024::10:54:28 === Ranch listener http, connection process <0.41180.0>, stream 1 had its request process <0.41182.0> exit with reason {response_error,payload_too_large,'204 and 304 responses must not include a body. (RFC7230 3.3)'} and stacktrace [{cowboy_req,do_reply_ensure_no_body,4,[{file,"src/cowboy_req.erl"},{line,832}]},{resp_h,do,3,[{file,"test/handlers/resp_h.erl"},{line,186}]},{cowboy_handler,execute,2,[{file,"src/cowboy_handler.erl"},{line,37}]},{cowboy_stream_h,execute,3,[{file,"src/cowboy_stream_h.erl"},{line,306}]},{cowboy_stream_h,request_process,3,[{file,"src/cowboy_stream_h.erl"},{line,295}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,241}]}]
Closing, thanks!