benoitc / couchbeam

Apache CouchDB client in Erlang
Other
242 stars 113 forks source link

couchbeam:reply_att/1 Should handle as success all 20x Response codes? #114

Closed epappas closed 9 years ago

epappas commented 9 years ago

In case of streaming, couchDB sends a 201code as a success. Though in the following code snippet

send_attachment(Ref, eof) ->
    case hackney:finish_send_body(Ref) of
        ok ->
            Resp =  hackney:start_response(Ref),
            reply_att(Resp); %% <<----- Mind here
        Error ->
            Error
    end;
reply_att({ok, 200, _, Ref}) ->
    {[{<<"ok">>, true}|R]} = couchbeam_httpc:json_body(Ref),
            {ok, {R}};
......
reply_att({ok, Status, Headers, Ref}) ->
    {ok, Body} = hackney:body(Ref),
    {error, {bad_response, {Status, Headers, Body}}};

reply_att/1 Handles only 200 as a success code. So in my case I had the following response when I called couchbeam:send_attachment(Ref, eof)

{error,{bad_response,{201,
    [{<<"Server">>,<<"CouchDB/1.6.1 (Erlang OTP/17)">>},
        {<<"Location">>, <<"http://localhost:5984/testupload/1234/test">>},
        {<<"ETag">>, <<"\"1-8370d85c306eaf1b7a23338ef836fe01\"">>},
        {<<"Date">>,<<"Sat, 01 Nov 2014 14:30:43 GMT">>},
        {<<"Content-Type">>,<<"application/json">>},
        {<<"Content-Length">>,<<"67">>},
        {<<"Cache-Control">>,<<"must-revalidate">>}],
    <<"{\"ok\":true,\"id\":\"1234\",\"rev\":\"1-8370d85c306eaf1b7a23338ef836fe01\"}\n">>}}}

Maybe a {ok, {R}} would be a better fit?

Cheers.

epappas commented 9 years ago

Maybe the same should apply to 30x & 10x codes? That's just a simple suggestion I don't think it's a "must".

benoitc commented 9 years ago

fixed in latest head.