Skip to content

Commit e830b03

Browse files
authored
Merge pull request #4308 from esl/return-error-when-sql-query-fails
Return error IQ when SQL query execution fails
2 parents 11c33c3 + 35dfa47 commit e830b03

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
lines changed

big_tests/tests/mam_SUITE.erl

+60-2
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,8 @@ muc_light_cases() ->
424424
muc_light_include_groupchat_messages_by_default,
425425
muc_light_chat_markers_are_archived_if_enabled,
426426
muc_light_chat_markers_are_not_archived_if_disabled,
427-
muc_light_failed_to_decode_message_in_database
427+
muc_light_failed_to_decode_message_in_database,
428+
muc_light_sql_query_failed
428429
].
429430

430431
muc_rsm_cases() ->
@@ -487,7 +488,8 @@ prefs_cases() ->
487488

488489
impl_specific() ->
489490
[check_user_exist,
490-
pm_failed_to_decode_message_in_database].
491+
pm_failed_to_decode_message_in_database,
492+
pm_sql_query_failed].
491493

492494
suite() ->
493495
require_rpc_nodes([mim]) ++ escalus:suite().
@@ -706,12 +708,34 @@ end_state(_, _, Config) ->
706708
init_per_testcase(CaseName, Config) ->
707709
case maybe_skip(CaseName, Config) of
708710
ok ->
711+
maybe_setup_meck(CaseName),
709712
dynamic_modules:ensure_modules(host_type(), required_modules(CaseName, Config)),
710713
lists:foldl(fun(StepF, ConfigIn) -> StepF(CaseName, ConfigIn) end, Config, init_steps());
711714
{skip, Msg} ->
712715
{skip, Msg}
713716
end.
714717

718+
maybe_setup_meck(muc_light_sql_query_failed) ->
719+
ok = rpc(mim(), meck, new, [mongoose_rdbms, [no_link, passthrough]]),
720+
ok = rpc(mim(), meck, expect,
721+
[mongoose_rdbms, execute_successfully,
722+
fun (_HostType, mam_muc_message_lookup_a_equ_limit, _Parameters) ->
723+
error(#{what => simulated_error});
724+
(HostType, Name, Parameters) ->
725+
meck:passthrough([HostType, Name, Parameters])
726+
end]);
727+
maybe_setup_meck(pm_sql_query_failed) ->
728+
ok = rpc(mim(), meck, new, [mongoose_rdbms, [no_link, passthrough]]),
729+
ok = rpc(mim(), meck, expect,
730+
[mongoose_rdbms, execute_successfully,
731+
fun (_HostType, mam_message_lookup_a_equ_limit, _Parameters) ->
732+
error(#{what => simulated_error});
733+
(HostType, Name, Parameters) ->
734+
meck:passthrough([HostType, Name, Parameters])
735+
end]);
736+
maybe_setup_meck(_) ->
737+
ok.
738+
715739
init_steps() ->
716740
[fun init_users/2, fun init_archive/2, fun start_room/2, fun init_metrics/2,
717741
fun escalus:init_per_testcase/2].
@@ -733,6 +757,11 @@ maybe_skip(C, Config) when C =:= muc_light_failed_to_decode_message_in_database;
733757
C =:= pm_failed_to_decode_message_in_database ->
734758
skip_if(?config(configuration, Config) =:= elasticsearch,
735759
"elasticsearch does not support encodings");
760+
maybe_skip(C, Config) when C =:= muc_light_sql_query_failed;
761+
C =:= pm_sql_query_failed ->
762+
skip_if(?config(configuration, Config) =:= elasticsearch orelse
763+
?config(configuration, Config) =:= cassandra,
764+
"Not an SQL database");
736765
maybe_skip(C, Config) when C =:= muc_light_include_groupchat_filter;
737766
C =:= muc_light_no_pm_stored_include_groupchat_filter;
738767
C =:= muc_light_include_groupchat_messages_by_default ->
@@ -840,6 +869,10 @@ init_metrics(muc_archive_request, Config) ->
840869
init_metrics(_CaseName, Config) ->
841870
Config.
842871

872+
end_per_testcase(CaseName, Config) when CaseName =:= pm_sql_query_failed;
873+
CaseName =:= muc_light_sql_query_failed ->
874+
teardown_meck(),
875+
escalus:end_per_testcase(CaseName, Config);
843876
end_per_testcase(CaseName = muc_validate_mam_id, Config) ->
844877
unmock_mongoose_mam_id(mim()),
845878
escalus:end_per_testcase(CaseName, Config);
@@ -857,6 +890,9 @@ all_cases_with_room() ->
857890
muc_cases_with_room() ++ muc_fetch_specific_msgs_cases() ++ muc_configurable_archiveid_cases() ++
858891
muc_stanzaid_cases() ++ muc_retract_cases() ++ muc_metadata_cases() ++ muc_text_search_cases().
859892

893+
teardown_meck() ->
894+
rpc(mim(), meck, unload, []).
895+
860896
%% Module configuration per testcase
861897

862898
required_modules(CaseName, Config) when CaseName =:= muc_light_service_discovery_stored_in_pm;
@@ -1846,6 +1882,19 @@ muc_light_failed_to_decode_message_in_database(Config) ->
18461882
assert_failed_to_decode_message(ArcMsg)
18471883
end).
18481884

1885+
muc_light_sql_query_failed(Config) ->
1886+
escalus:story(Config, [{alice, 1}], fun(Alice) ->
1887+
Room = muc_helper:fresh_room_name(),
1888+
given_muc_light_room(Room, Alice, []),
1889+
M1 = when_muc_light_message_is_sent(Alice, Room,
1890+
<<"Msg 1">>, <<"Id1">>),
1891+
then_muc_light_message_is_received_by([Alice], M1),
1892+
mam_helper:wait_for_room_archive_size(muc_light_host(), Room, 2),
1893+
when_archive_query_is_sent(Alice, muc_light_helper:room_bin_jid(Room), Config),
1894+
Error = escalus:wait_for_stanza(Alice),
1895+
escalus:assert(is_error, [<<"wait">>, <<"internal-server-error">>], Error)
1896+
end).
1897+
18491898
pm_failed_to_decode_message_in_database(Config) ->
18501899
escalus:fresh_story(Config, [{alice, 1}, {bob, 1}], fun(Alice, Bob) ->
18511900
escalus:send(Alice, escalus_stanza:chat_to(Bob, <<"Hi">>)),
@@ -1858,6 +1907,15 @@ pm_failed_to_decode_message_in_database(Config) ->
18581907
assert_failed_to_decode_message(ArcMsg)
18591908
end).
18601909

1910+
pm_sql_query_failed(Config) ->
1911+
escalus:fresh_story(Config, [{alice, 1}, {bob, 1}], fun(Alice, Bob) ->
1912+
escalus:send(Alice, escalus_stanza:chat_to(Bob, <<"OH, HAI!">>)),
1913+
mam_helper:wait_for_archive_size(Alice, 1),
1914+
when_archive_query_is_sent(Alice, undefined, Config),
1915+
Error = escalus:wait_for_stanza(Alice),
1916+
escalus:assert(is_error, [<<"wait">>, <<"internal-server-error">>], Error)
1917+
end).
1918+
18611919
retrieve_form_fields(ConfigIn) ->
18621920
escalus_fresh:story(ConfigIn, [{alice, 1}], fun(Alice) ->
18631921
P = ?config(props, ConfigIn),

src/hooks/mongoose_hooks.erl

+2-2
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ mam_remove_archive(HostType, ArchiveID, Owner) ->
989989
-spec mam_lookup_messages(HostType, Params) -> Result when
990990
HostType :: mongooseim:host_type(),
991991
Params :: map(),
992-
Result :: {ok, mod_mam:lookup_result()}.
992+
Result :: {ok, mod_mam:lookup_result()} | {error, Reason :: term()}.
993993
mam_lookup_messages(HostType, Params) ->
994994
InitialLookupValue = {0, 0, []}, %% mod_mam:lookup_result() type
995995
run_hook_for_host_type(mam_lookup_messages, HostType, {ok, InitialLookupValue},
@@ -1118,7 +1118,7 @@ mam_muc_remove_archive(HostType, ArchiveID, Room) ->
11181118
-spec mam_muc_lookup_messages(HostType, Params) -> Result when
11191119
HostType :: mongooseim:host_type(),
11201120
Params :: map(),
1121-
Result :: {ok, mod_mam:lookup_result()}.
1121+
Result :: {ok, mod_mam:lookup_result()} | {error, Reason :: term()}.
11221122
mam_muc_lookup_messages(HostType, Params) ->
11231123
InitialLookupValue = {0, 0, []}, %% mod_mam:lookup_result() type
11241124
run_hook_for_host_type(mam_muc_lookup_messages, HostType, {ok, InitialLookupValue},

src/mam/mod_mam_muc_rdbms_arch.erl

+5-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,11 @@ lookup_messages(_Result, #{owner_jid := ArcJID} = Params, #{host_type := HostTyp
383383
Env = env_vars(HostType, ArcJID),
384384
ExdParams = mam_encoder:extend_lookup_params(Params, Env),
385385
Filter = mam_filter:produce_filter(ExdParams, lookup_fields()),
386-
{ok, mam_lookup:lookup(Env, Filter, ExdParams)}.
386+
try
387+
{ok, mam_lookup:lookup(Env, Filter, ExdParams)}
388+
catch _Type:Reason ->
389+
{ok, {error, Reason}}
390+
end.
387391

388392
lookup_query(QueryType, Env, Filters, Order, OffsetLimit) ->
389393
mam_lookup_sql:lookup_query(QueryType, Env, Filters, Order, OffsetLimit).

src/mam/mod_mam_rdbms_arch.erl

+5-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,11 @@ lookup_messages(_Result, #{owner_jid := ArcJID} = Params, #{host_type := HostTyp
407407
Env = env_vars(HostType, ArcJID),
408408
ExdParams = mam_encoder:extend_lookup_params(Params, Env),
409409
Filter = mam_filter:produce_filter(ExdParams, lookup_fields()),
410-
{ok, mam_lookup:lookup(Env, Filter, ExdParams)}.
410+
try
411+
{ok, mam_lookup:lookup(Env, Filter, ExdParams)}
412+
catch _Type:Reason ->
413+
{ok, {error, Reason}}
414+
end.
411415

412416
lookup_query(QueryType, Env, Filters, Order, OffsetLimit) ->
413417
mam_lookup_sql:lookup_query(QueryType, Env, Filters, Order, OffsetLimit).

0 commit comments

Comments
 (0)