From d030946efd58ed60319c217d3acbfad9926f0166 Mon Sep 17 00:00:00 2001 From: Luca Favatella Date: Thu, 23 Feb 2017 17:05:37 +0000 Subject: [PATCH] Keep information on errors while reporting them 1. Report wrong datatype of the bucket if key not found rather then considering the error as plain "key not found" error. * This also improves readibility by: * Clarifying what the value discarded before error reporting is; * Hardcoding the expectation of the module of map data type. * See also: * https://github.com/basho/riak-erlang-client/blob/2.5.2/src/riakc_pb_socket.erl#L1262-L1264 * https://github.com/basho/riak-erlang-client/blob/2.5.2/src/riakc_datatype.erl#L94 2. Tag errors while streaming keys as such, because caller may not expect key streaming error while having requested apparently completely separate operation. 3. Keep whether streaming error is timeout or other error; 4. Hardcode expectation of no continuation in key stream, because no max number of results specificied when requesting key stream in the first place. * Refs: * https://github.com/basho/riak-erlang-client/blob/2.5.2/src/riakc_pb_socket.erl#L1019-L1023 * https://github.com/basho/riak-erlang-client/blob/2.5.2/src/riakc_pb_socket.erl#L1806-L1814 * https://github.com/basho/riak-erlang-client/blob/2.5.2/include/riakc.hrl#L151-L156 * https://github.com/basho/riak_test/blob/743d5dfb20717a81bb79392522c0581309205d76/tests/secondary_index_tests.erl#L204-L235 5. Prefer `exit` over `throw` in order to keep stacktrace. * See also https://github.com/inaka/erlang_guidelines/pull/71 6. Keep most information on errors while persisting. * Function for persisting has room for improvements - out of scope for this commit though. Also perform minor clarification of variable name in stream reception. ---- These changes are beneficial when attempting to determing why something does not work. E.g. if a user attempt to get started on sumo_db_riak with a Riak configured with bitcask backend rather than leveldb then these changes make the error be like the following (this is an excerpt from a test suite run - note strings `stream_keys`, `indexes_not_supported,riak_kv_bitcask_backend`): ``` %%% basic_SUITE ==> find: SKIPPED %%% basic_SUITE ==> {tc_auto_skip, {auto_skipped, {failed, {basic_SUITE,init_per_testcase, {{error, {stream_keys, {unexpected, {82448287, {error, <<"{error,{indexes_not_supported,riak_kv_bitcask_backend}}">>}}}, 0}}, [{sumo,delete_all,1, [{file, "/Users/luca/dev/vuk/inaka_crud/sumo_db_riak/_build/default/lib/sumo_db/src/sumo.erl"}, {line,227}]}, {sumo_basic_test_helper,init_store,1, [{file, "/Users/luca/dev/vuk/inaka_crud/sumo_db_riak/_build/default/lib/sumo_db/src/adapter_test_helpers/sumo_basic_test_helper.erl"}, {line,165}]}, {basic_SUITE,init_per_testcase,2, [{file, "/Users/luca/dev/vuk/inaka_crud/sumo_db_riak/_build/test/lib/sumo_db_riak/test/basic_SUITE.erl"}, {line,51}]}, {test_server,do_init_per_testcase,2, [{file,"test_server.erl"},{line,1297}]}, {test_server,run_test_case_eval1,6, [{file,"test_server.erl"},{line,1007}]}, {test_server,run_test_case_eval,9, [{file,"test_server.erl"},{line,977}]}]}}}}} ``` --- src/sumo_store_riak.erl | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/sumo_store_riak.erl b/src/sumo_store_riak.erl index 48eb3d8..04f94ba 100644 --- a/src/sumo_store_riak.erl +++ b/src/sumo_store_riak.erl @@ -147,7 +147,7 @@ fetch(DocName, Id, State) -> case fetch_map(Conn, Bucket, sumo_utils:to_bin(Id), Opts) of {ok, RMap} -> {ok, rmap_to_doc(DocName, RMap), State}; - {error, {notfound, _}} -> + {error, {notfound, _Type = map}} -> {error, notfound, State}; {error, Error} -> {error, Error, State} @@ -202,8 +202,8 @@ delete_all(_DocName, State) -> Acc + length(Kst) end, case stream_keys(Conn, Bucket, Del, 0) of - {ok, Count} -> {ok, Count, State}; - {_, Count} -> {error, Count, State} + {ok, Count} -> {ok, Count, State}; + {error, Reason, Count} -> {error, {stream_keys, Reason, Count}, State} end. -spec find_all(DocName, State) -> Response when @@ -216,8 +216,8 @@ find_all(DocName, State) -> fetch_docs(DocName, Conn, Bucket, Kst, Opts) ++ Acc end, case stream_keys(Conn, Bucket, Get, []) of - {ok, Docs} -> {ok, Docs, State}; - {_, Docs} -> {error, Docs, State} + {ok, Docs} -> {ok, Docs, State}; + {error, Reason, Count} -> {error, {stream_keys, Reason, Count}, State} end. -spec find_all(DocName, SortFields, Limit, Offset, State) -> Response when @@ -489,8 +489,8 @@ new_doc(Doc, #state{conn = Conn, bucket = Bucket, put_opts = Opts}) -> undefined -> case update_map(Conn, Bucket, undefined, doc_to_rmap(Doc), Opts) of {ok, RiakMapId} -> RiakMapId; - {error, Error} -> throw(Error); - _ -> throw(unexpected) + {error, Error} -> exit(Error); + Unexpected -> exit({unexpected, Unexpected}) end; Id0 -> sumo_utils:to_bin(Id0) @@ -549,14 +549,14 @@ stream_keys(Conn, Bucket, F, Acc) -> %% @private receive_stream(Ref, F, Acc) -> receive - {Ref, {_, Stream, _}} -> - receive_stream(Ref, F, F(Stream, Acc)); - {Ref, {done, _}} -> + {Ref, {_, Keys, _}} -> + receive_stream(Ref, F, F(Keys, Acc)); + {Ref, {done, _Continuation = undefined}} -> {ok, Acc}; - _ -> - {error, Acc} + Unexpected -> + {error, {unexpected, Unexpected}, Acc} after - 30000 -> {timeout, Acc} + 30000 -> {error, timeout, Acc} end. %% @private