Skip to content
Permalink
Browse files
Fix nasty 500 error on POST to /_config
  • Loading branch information
wenwl authored and jiangphcn committed Nov 13, 2018
1 parent e7f2cb3 commit 62a5e5ec9efd181d3d57c27762a4b1741bd7116d
Showing 3 changed files with 59 additions and 9 deletions.
@@ -253,34 +253,45 @@ handle_call({set, Sec, Key, Val, Persist, Reason}, _From, Config) ->
true = ets:insert(?MODULE, {{Sec, Key}, Val}),
couch_log:notice("~p: [~s] ~s set to ~s for reason ~p",
[?MODULE, Sec, Key, Val, Reason]),
case {Persist, Config#config.write_filename} of
ConfigWriteReturn = case {Persist, Config#config.write_filename} of
{true, undefined} ->
ok;
{true, FileName} ->
config_writer:save_to_file({{Sec, Key}, Val}, FileName);
_ ->
ok
end,
Event = {config_change, Sec, Key, Val, Persist},
gen_event:sync_notify(config_event, Event),
{reply, ok, Config}
case ConfigWriteReturn of
ok ->
Event = {config_change, Sec, Key, Val, Persist},
gen_event:sync_notify(config_event, Event),
{reply, ok, Config};
{error, Else} ->
{reply, {error, Else}, Config}
end
end;

handle_call({delete, Sec, Key, Persist, Reason}, _From, Config) ->
true = ets:delete(?MODULE, {Sec,Key}),
couch_log:notice("~p: [~s] ~s deleted for reason ~p",
[?MODULE, Sec, Key, Reason]),
case {Persist, Config#config.write_filename} of
ConfigDeleteReturn = case {Persist, Config#config.write_filename} of
{true, undefined} ->
ok;
{true, FileName} ->
config_writer:save_to_file({{Sec, Key}, ""}, FileName);
_ ->
ok
end,
Event = {config_change, Sec, Key, deleted, Persist},
gen_event:sync_notify(config_event, Event),
{reply, ok, Config};
case ConfigDeleteReturn of
ok ->
Event = {config_change, Sec, Key, deleted, Persist},
gen_event:sync_notify(config_event, Event),
{reply, ok, Config};
Else ->
{reply, Else, Config}
end;

handle_call(reload, _From, Config) ->
DiskKVs = lists:foldl(fun(IniFile, DiskKVs0) ->
{ok, ParsedIniValues} = parse_ini_file(IniFile),
@@ -35,7 +35,7 @@ save_to_file({{Section, Key}, Value}, File) ->

NewLines = process_file_lines(Lines, [], SectionLine, Pattern, Key, Value),
NewFileContents = reverse_and_add_newline(strip_empty_lines(NewLines), []),
ok = file:write_file(File, NewFileContents).
file:write_file(File, NewFileContents).


process_file_lines([Section|Rest], SeenLines, Section, Pattern, Key, Value) ->
@@ -297,6 +297,45 @@ config_notifier_behaviour_test_() ->
}.


config_access_right_test_() ->
{
"Test config file access right",
{
foreach,
fun setup/0,
fun teardown/1,
[
fun should_write_config_to_file/0,
fun should_delete_config_from_file/0,
fun should_not_write_config_to_file/0,
fun should_not_delete_config_from_file/0
]
}
}.


should_write_config_to_file() ->
?assertEqual(ok, config:set("admins", "foo", "500", true)).


should_delete_config_from_file() ->
?assertEqual(ok, config:delete("admins", "foo", true)).


should_not_write_config_to_file() ->
meck:new(config_writer),
meck:expect(config_writer, save_to_file, fun(_, _) -> {error, eacces} end),
?assertEqual({error, eacces}, config:set("admins", "foo", "500", true)),
meck:unload(config_writer).


should_not_delete_config_from_file() ->
meck:new(config_writer),
meck:expect(config_writer, save_to_file, fun(_, _) -> {error, eacces} end),
?assertEqual({error, eacces}, config:delete("admins", "foo", true)),
meck:unload(config_writer).


should_load_all_configs() ->
?assert(length(config:all()) > 0).

0 comments on commit 62a5e5e

Please sign in to comment.