couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From e...@apache.org
Subject couch commit: updated refs/heads/master to b4295bf
Date Thu, 17 Mar 2016 16:05:35 GMT
Repository: couchdb-couch
Updated Branches:
  refs/heads/master 85d54067a -> b4295bfe5


Fix log_response

After recent refactoring `log_response/2` can also receive
an unencoded JSON as part of the error message.


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/b4295bfe
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/b4295bfe
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/b4295bfe

Branch: refs/heads/master
Commit: b4295bfe58680c6a3d332a73ac6b061bd78b4db3
Parents: 85d5406
Author: Eric Avdey <eiri@eiri.ca>
Authored: Wed Mar 16 16:45:00 2016 -0300
Committer: Eric Avdey <eiri@eiri.ca>
Committed: Thu Mar 17 12:50:48 2016 -0300

----------------------------------------------------------------------
 src/couch_httpd.erl | 52 +++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 45 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/b4295bfe/src/couch_httpd.erl
----------------------------------------------------------------------
diff --git a/src/couch_httpd.erl b/src/couch_httpd.erl
index 40f5c99..7370594 100644
--- a/src/couch_httpd.erl
+++ b/src/couch_httpd.erl
@@ -649,16 +649,17 @@ log_request(#httpd{mochi_req=MochiReq,peer=Peer}=Req, Code) ->
             gen_event:notify(couch_plugin, {log_request, Req, Code})
     end.
 
+log_response(Code, _) when Code < 400 ->
+    ok;
 log_response(Code, Body) ->
-    case erlang:get(dont_log_response) of
-        true ->
+    case {erlang:get(dont_log_response), Body} of
+        {true, _} ->
             ok;
-        _ when Code >= 500 ->
-            couch_log:error("httpd ~p error response:~n ~s", [Code, Body]);
-        _ when Code >= 400 ->
-            couch_log:error("httpd ~p error response:~n ~s", [Code, Body]);
+        {_, {json, JsonObj}} ->
+            ErrorMsg = couch_util:json_encode(JsonObj),
+            couch_log:error("httpd ~p error response:~n ~s", [Code, ErrorMsg]);
         _ ->
-            ok
+            couch_log:error("httpd ~p error response:~n ~s", [Code, Body])
     end.
 
 start_response_length(#httpd{mochi_req=MochiReq}=Req, Code, Headers0, Length) ->
@@ -1177,4 +1178,41 @@ maybe_add_default_headers_test_() ->
     end, Cases),
     {"Tests adding default headers", Tests}.
 
+log_request_test_() ->
+    {foreachx,
+        fun(_) ->
+            ok = meck:new([couch_log]),
+            ok = meck:expect(couch_log, error, fun(Fmt, Args) ->
+                case catch io_lib_format:fwrite(Fmt, Args) of
+                    {'EXIT', Error} -> Error;
+                    _ -> ok
+                end
+            end)
+        end,
+        fun(_, _) ->
+            meck:unload([couch_log])
+        end,
+        [{Flag, fun should_accept_code_and_message/2} || Flag <- [true, false]]
+    }.
+
+should_accept_code_and_message(DontLogFlag, _) ->
+    erlang:put(dont_log_response, DontLogFlag),
+    {"with dont_log_response = " ++ atom_to_list(DontLogFlag),
+        [
+            {"Should accept code 200 and string message",
+            ?_assertEqual(ok, log_response(200, "OK"))},
+            {"Should accept code 200 and JSON message",
+            ?_assertEqual(ok, log_response(200, {json, {[{ok, true}]}}))},
+            {"Should accept code >= 400 and string error",
+            ?_assertEqual(ok, log_response(405, method_not_allowed))},
+            {"Should accept code >= 400 and JSON error",
+            ?_assertEqual(ok,
+                log_response(405, {json, {[{error, method_not_allowed}]}}))},
+            {"Should accept code >= 500 and string error",
+            ?_assertEqual(ok, log_response(500, undef))},
+            {"Should accept code >= 500 and JSON error",
+            ?_assertEqual(ok, log_response(500, {json, {[{error, undef}]}}))}
+        ]
+    }.
+
 -endif.


Mime
View raw message