Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

#8 Unify errors when payload is missing or json decoder fails.

  • Loading branch information...
commit f4685b778b7048b773831bac234f019437d76e06 1 parent 6ccbe47
@majek majek authored
Showing with 30 additions and 12 deletions.
  1. +23 −10 src/sockjs_filters.erl
  2. +7 −2 src/sockjs_util.erl
View
33 src/sockjs_filters.erl
@@ -216,21 +216,35 @@ options(Req, Headers, _Server, _SessionId) ->
sockjs_http:reply(204, Headers, "", Req).
%% --------------------------------------------------------------------------
-
%% This is send but it receives - "send" from the client POV, receive
%% from ours.
xhr_send(Req, Headers, _Server, SessionId, Receive) ->
{Body, Req1} = sockjs_http:body(Req),
- receive_body(Body, SessionId, Receive),
- %% FF assumes that the response is XML.
- sockjs_http:reply(204,
- [{"content-type", "text/plain"}] ++ Headers, "", Req1).
+ Success = fun () -> sockjs_http:reply(204,
+ [{"content-type", "text/plain"}] ++
+ Headers, "", Req1) end,
+ verify_body(Req1, Body, SessionId, Receive, Success).
jsonp_send(Req, Headers, _Server, SessionId, Receive) ->
{Body, Req1} = sockjs_http:body_qs(Req),
- receive_body(Body, SessionId, Receive),
- sockjs_http:reply(200, Headers, "", Req1).
-
+ Success = fun () -> sockjs_http:reply(200, Headers, "ok", Req) end,
+ verify_body(Req1, Body, SessionId, Receive, Success).
+
+verify_body(Req, Body, SessionId, Receive, Success) ->
+ case Body of
+ E when E =:= undefined orelse E =:= [] orelse E =:= <<>> ->
+ sockjs_http:reply(500, [],
+ "Payload expected.", Req);
+ _ ->
+ case sockjs_util:decode(Body) of
+ {ok, Decoded} ->
+ receive_body(Decoded, SessionId, Receive),
+ Success();
+ {error, _} ->
+ sockjs_http:reply(500, [],
+ "Broken JSON encoding.", Req)
+ end
+ end.
%% --------------------------------------------------------------------------
cache_for(_Req, Headers, _Server, _SessionId) ->
@@ -279,8 +293,7 @@ expect_form(_Req, Headers, _Server, _SessionId) ->
%% --------------------------------------------------------------------------
-receive_body(Body, SessionId, Receive) ->
- Decoded = sockjs_util:decode(Body),
+receive_body(Decoded, SessionId, Receive) ->
Sender = sockjs_session:sender(SessionId),
[Receive(Sender, {recv, Msg}) || Msg <- Decoded].
View
9 src/sockjs_util.erl
@@ -23,7 +23,12 @@ decode(Thing) ->
sockjs_util:Encoder(Thing, decode).
mochijson2(Thing, encode) -> iolist_to_binary(mochijson2:encode(Thing));
-mochijson2(JSON, decode) -> mochijson2:decode(JSON).
+mochijson2(JSON, decode) ->
+ try mochijson2:decode(JSON) of
+ V -> {ok, V}
+ catch
+ E -> {error, E}
+ end.
eep0018(Thing, encode) -> {ok, JSON} = json:encode(Thing), JSON;
-eep0018(JSON, decode) -> {ok, Thing} = json:decode(JSON), Thing.
+eep0018(JSON, decode) -> json:decode(JSON).
Please sign in to comment.
Something went wrong with that request. Please try again.