This is an automated email from the ASF dual-hosted git repository.

jiahuili430 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit a47f9b33c5f304e74b57de4a6a07a9518270142c
Author: Jiahui Li <[email protected]>
AuthorDate: Fri Oct 3 08:56:30 2025 -0500

    Fix `case_clause` when got `missing_target` error
    
    When mem3 tries to get or create a database, it throws a `missing_target`
    error, but it's essentially a `database_does_not_exist` error. So convert
    it back in fabric_rpc.erl and try to fix the case_clause error if it occurs.
    
    Error log:
    
    ```log
    
{{case_clause,{error,{error,missing_target,[{mem3_util,get_or_create_db_int,2,[{file,"src/mem3_util.erl"},{line,627}]},{fabric_rpc,all_docs,3,[{file,"src/fabric_rpc.erl"},{line,157}]},{rexi_server,init_p,3,[{file,"src/rexi_server.erl"},{line,141}]}]}}},[{ken_server,update_db_indexes,2,[{file,"src/ken_server.erl"},{line,270}]}]}#012
    ```
---
 src/fabric/src/fabric_rpc.erl              | 39 +++++++++++++++++++++---------
 src/fabric/test/eunit/fabric_rpc_tests.erl |  2 +-
 2 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/src/fabric/src/fabric_rpc.erl b/src/fabric/src/fabric_rpc.erl
index 492546b90..86f911905 100644
--- a/src/fabric/src/fabric_rpc.erl
+++ b/src/fabric/src/fabric_rpc.erl
@@ -154,9 +154,13 @@ all_docs(DbName, Options, Args0) ->
     case fabric_util:upgrade_mrargs(Args0) of
         #mrargs{keys = undefined} = Args ->
             set_io_priority(DbName, Options),
-            {ok, Db} = get_or_create_db(DbName, Options),
-            CB = get_view_cb(Args),
-            couch_mrview:query_all_docs(Db, Args, CB, Args)
+            case get_or_create_db(DbName, Options) of
+                {ok, Db} ->
+                    CB = get_view_cb(Args),
+                    couch_mrview:query_all_docs(Db, Args, CB, Args);
+                Error ->
+                    rexi:reply(Error)
+            end
     end.
 
 update_mrview(DbName, {DDocId, Rev}, ViewName, Args0) ->
@@ -179,9 +183,13 @@ map_view(DbName, {DDocId, Rev}, ViewName, Args0, 
DbOptions) ->
 map_view(DbName, DDoc, ViewName, Args0, DbOptions) ->
     set_io_priority(DbName, DbOptions),
     Args = fabric_util:upgrade_mrargs(Args0),
-    {ok, Db} = get_or_create_db(DbName, DbOptions),
-    CB = get_view_cb(Args),
-    couch_mrview:query_view(Db, DDoc, ViewName, Args, CB, Args).
+    case get_or_create_db(DbName, DbOptions) of
+        {ok, Db} ->
+            CB = get_view_cb(Args),
+            couch_mrview:query_view(Db, DDoc, ViewName, Args, CB, Args);
+        Error ->
+            rexi:reply(Error)
+    end.
 
 %% @equiv reduce_view(DbName, DDoc, ViewName, Args0)
 reduce_view(DbName, DDocInfo, ViewName, Args0) ->
@@ -193,10 +201,14 @@ reduce_view(DbName, {DDocId, Rev}, ViewName, Args0, 
DbOptions) ->
 reduce_view(DbName, DDoc, ViewName, Args0, DbOptions) ->
     set_io_priority(DbName, DbOptions),
     Args = fabric_util:upgrade_mrargs(Args0),
-    {ok, Db} = get_or_create_db(DbName, DbOptions),
-    VAcc0 = #vacc{db = Db},
-    Callback = fun(Msg, Acc) -> reduce_cb(Msg, Acc, Args#mrargs.extra) end,
-    couch_mrview:query_view(Db, DDoc, ViewName, Args, Callback, VAcc0).
+    case get_or_create_db(DbName, DbOptions) of
+        {ok, Db} ->
+            VAcc0 = #vacc{db = Db},
+            Callback = fun(Msg, Acc) -> reduce_cb(Msg, Acc, Args#mrargs.extra) 
end,
+            couch_mrview:query_view(Db, DDoc, ViewName, Args, Callback, VAcc0);
+        Error ->
+            rexi:reply(Error)
+    end.
 
 create_db(DbName) ->
     create_db(DbName, []).
@@ -499,7 +511,12 @@ get_node_seqs(Db, Nodes) ->
     [{binary_to_existing_atom(N), S} || {N, S} <- NodeBinSeqs].
 
 get_or_create_db(DbName, Options) ->
-    mem3_util:get_or_create_db_int(DbName, Options).
+    try
+        mem3_util:get_or_create_db_int(DbName, Options)
+    catch
+        throw:{error, missing_target} ->
+            erlang:error(database_does_not_exist, [DbName])
+    end.
 
 get_view_cb(#mrargs{extra = Options}) ->
     case couch_util:get_value(callback, Options) of
diff --git a/src/fabric/test/eunit/fabric_rpc_tests.erl 
b/src/fabric/test/eunit/fabric_rpc_tests.erl
index 16bb66bad..39c86e4c4 100644
--- a/src/fabric/test/eunit/fabric_rpc_tests.erl
+++ b/src/fabric/test/eunit/fabric_rpc_tests.erl
@@ -101,7 +101,7 @@ t_no_config_db_create_fails_for_shard_rpc(DbName) ->
         receive
             Resp0 -> Resp0
         end,
-    ?assertMatch({Ref, {'rexi_EXIT', {{error, missing_target}, _}}}, Resp).
+    ?assertMatch({Ref, {'rexi_EXIT', {database_does_not_exist, _}}}, Resp).
 
 t_db_create_with_config(DbName) ->
     MDbName = mem3:dbname(DbName),

Reply via email to