Skip to content

Commit

Permalink
Added Error Handling to Range Get
Browse files Browse the repository at this point in the history
  • Loading branch information
windkit committed Jun 10, 2015
1 parent d52c9b2 commit 6d85afd
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions src/leo_gateway_http_commons.erl
Original file line number Diff line number Diff line change
Expand Up @@ -753,8 +753,12 @@ get_range_object(Req, Bucket, Key, {_Unit, Range}) when is_list(Range) ->
?reply_bad_range([?SERVER_HEADER], Key, <<>>, Req);
{error, unavailable} ->
?reply_service_unavailable_error([?SERVER_HEADER], Key, <<>>, Req);
{error, timeout} ->
?reply_timeout_without_body([?SERVER_HEADER], Req);
{error, ?ERR_TYPE_INTERNAL_ERROR} ->
?reply_internal_error_without_body([?SERVER_HEADER], Req);
_ ->
?reply_not_found([?SERVER_HEADER], Key, <<>>, Req)
?reply_not_found_without_body([?SERVER_HEADER], Req)
end.

get_body_length_1(Key, Range) ->
Expand All @@ -780,10 +784,19 @@ get_body_length([End|Rest], ObjectSize, Acc) when End < ObjectSize ->
get_body_length(_, _, _) ->
{error, bad_range}.

get_range_object_1(_Req, _Bucket, _Key, _, {error, _Reason}, Socket, Transport) ->
Transport:close(Socket);
% Transport:close(Socket),
% case Reason of
% unavailable ->
% ?reply_service_unavailable_error([?SERVER_HEADER], Key, <<>>, Req);
% not_found ->
% ?reply_not_found([?SERVER_HEADER], Key, <<>>, Req);
% _ ->
% ?reply_internal_error_without_body([?SERVER_HEADER], Req)
% end;
get_range_object_1(Req,_Bucket,_Key, [], _, _Socket, _Transport) ->
{ok, Req};
get_range_object_1(Req,_Bucket,_Key, _, {error, _}, _Socket, _Transport) ->
{ok, Req};
get_range_object_1(Req, Bucket, Key, [{Start, infinity}|Rest], _, Socket, Transport) ->
Ret = get_range_object_2(Req, Bucket, Key, Start, 0, Socket, Transport),
get_range_object_1(Req, Bucket, Key, Rest, Ret, Socket, Transport);
Expand Down Expand Up @@ -823,25 +836,26 @@ get_range_object_2(Req, Bucket, Key, Start, End, Socket, Transport) ->
get_range_object_large(Req, Bucket, Key,
NewStartPos, NewEndPos, N, Index_1, CurPos_1,
Socket, Transport);
{error, unavailable} ->
?reply_service_unavailable_error([?SERVER_HEADER], Key, <<>>, Req);
_ ->
{error, not_found}
Error ->
Error
end.


%% @doc Retrieve the small object
%% @private
get_range_object_small(Req, Bucket, Key, Start, End, Socket, Transport) ->
get_range_object_small(_Req, Bucket, Key, Start, End, Socket, Transport) ->
case leo_gateway_rpc_handler:get(Key, Start, End) of
{ok, _Meta, <<>>} ->
?access_log_get(Bucket, Key, 0, ?HTTP_ST_OK),
ok;
{ok, _Meta, Bin} ->
?access_log_get(Bucket, Key, byte_size(Bin), ?HTTP_ST_OK),
Transport:send(Socket, Bin);
{error, unavailable} ->
?reply_service_unavailable_error([?SERVER_HEADER], Key, <<>>, Req);
case Transport:send(Socket, Bin) of
ok ->
ok;
{error, Cause} ->
{error, Cause}
end;
{error, Cause} ->
{error, Cause}
end.
Expand All @@ -868,6 +882,8 @@ calc_pos(StartPos, EndPos, _ObjectSize) ->

%% @doc Retrieve the large object
%% @private
get_range_object_large(_Req,_Bucket,_Key,_Start,_End, _Total, _Index, {error, _} = Error, _Socket, _Transport) ->
Error;
get_range_object_large(_Req,_Bucket,_Key,_Start,_End, Total, Total, CurPos, _Socket, _Transport) ->
{ok, CurPos};
get_range_object_large(_Req,_Bucket,_Key,_Start, End,_Total,_Index, CurPos, _Socket, _Transport) when CurPos > End ->
Expand All @@ -890,8 +906,6 @@ get_range_object_large( Req, Bucket, Key, Start, End, Total, Index, CurPos, Sock
{error, Cause} ->
{error, Cause}
end;
{error, unavailable} ->
?reply_service_unavailable_error([?SERVER_HEADER], Key, <<>>, Req);
{error, Cause} ->
{error, Cause}
end.
Expand All @@ -908,8 +922,12 @@ send_chunk(_Req,_Bucket, Key, Start, End, CurPos, ChunkSize, Socket, Transport)
{ok, _Meta, Bin} ->
%% @FIXME current impl can't handle a file which consist of grand children
%% ?access_log_get(Bucket, Key, ChunkSize, ?HTTP_ST_OK),
Transport:send(Socket, Bin),
CurPos + ChunkSize;
case Transport:send(Socket, Bin) of
ok ->
CurPos + ChunkSize;
{error, Cause} ->
{error, Cause}
end;
Error ->
Error
end;
Expand All @@ -929,8 +947,12 @@ send_chunk(_Req, _Bucket, Key, Start, End, CurPos, ChunkSize, Socket, Transport)
{ok, _Meta, Bin} ->
%% @FIXME current impl can't handle a file which consist of grand childs
%% ?access_log_get(Bucket, Key, ChunkSize, ?HTTP_ST_OK),
Transport:send(Socket, Bin),
CurPos + ChunkSize;
case Transport:send(Socket, Bin) of
ok ->
CurPos + ChunkSize;
{error, Cause} ->
{error, Cause}
end;
{error, Cause} ->
{error, Cause}
end.
Expand Down

0 comments on commit 6d85afd

Please sign in to comment.