Added some more detailed responses to the JSON API calls.

main
absc 2024-08-11 20:49:48 +00:00
parent 2d0d384d1e
commit 2b530fbe7e
2 changed files with 11 additions and 8 deletions

View File

@ -146,7 +146,7 @@ resource_exists(Req, State) ->
#{dudename := User} = cowboy_req:match_cookies([dudename], Req), #{dudename := User} = cowboy_req:match_cookies([dudename], Req),
{ok, Bucket} = maps:find(bucket, State), {ok, Bucket} = maps:find(bucket, State),
case dudeswave_auth:user_details(User, Bucket) of case dudeswave_auth:details(User, Bucket) of
[] -> [] ->
Resp = json:encode(#{<<"error">> => <<"user does not exists">>}), Resp = json:encode(#{<<"error">> => <<"user does not exists">>}),
Req0 = cowboy_req:reply(404, #{}, Resp, Req), Req0 = cowboy_req:reply(404, #{}, Resp, Req),
@ -193,8 +193,8 @@ delete_completed(Req, State) -> {false, Req, State}.
% %
login(Req, State) -> login(Req, State) ->
{ok, Cookies} = maps:find(State, cookies), {ok, Cookies} = maps:find(cookies, State),
{ok, Bucket} = maps:find(State, bucket), {ok, Bucket} = maps:find(bucket, State),
{ok, Data, Req0} = cowboy_req:read_body(Req), {ok, Data, Req0} = cowboy_req:read_body(Req),
#{<<"user">> := User, <<"password">> := Pass} = json:decode(Data), #{<<"user">> := User, <<"password">> := Pass} = json:decode(Data),

View File

@ -182,7 +182,10 @@ forbidden(Req, State) ->
{ok, Bucket} = maps:find(cookies, State), {ok, Bucket} = maps:find(cookies, State),
case dudeswave_auth:authenticate({cookie, User, Auth}, Bucket) of case dudeswave_auth:authenticate({cookie, User, Auth}, Bucket) of
{error, service_unavailable} -> exit(service_unavailable); {error, service_unavailable} ->
Resp = json:encode(#{<<"error">> => <<"internal server error">>}),
Req0 = cowboy_req:reply(500, #{}, Resp, Req),
{true, Req0, State};
true -> true ->
Resp = json:encode(#{<<"error">> => <<"authentication required">>}), Resp = json:encode(#{<<"error">> => <<"authentication required">>}),
Req0 = cowboy_req:reply(403, #{}, Resp, Req), Req0 = cowboy_req:reply(403, #{}, Resp, Req),
@ -275,10 +278,10 @@ create_user(Req, State) ->
Resp = json:encode(#{<<"result">> => <<"created">>}), Resp = json:encode(#{<<"result">> => <<"created">>}),
Req0 = cowboy_req:reply(201, #{}, Resp, Req), Req0 = cowboy_req:reply(201, #{}, Resp, Req),
{true, Req0, []}; {true, Req0, []};
{error, Reason} -> {error, _} ->
Resp = json:encode(#{<<"error">> => <<"internal server error">>}), Resp = json:encode(#{<<"error">> => <<"internal server error">>}),
Req0 = cowboy_req:reply(500, #{}, Resp, Req), Req0 = cowboy_req:reply(500, #{}, Resp, Req),
{false, Req0, Reason} {false, Req0, State}
end. end.
modify_user(Req, State) -> modify_user(Req, State) ->
@ -293,10 +296,10 @@ modify_user(Req, State) ->
Resp = json:encode(#{<<"result">> => <<"details updated">>}), Resp = json:encode(#{<<"result">> => <<"details updated">>}),
Req0 = cowboy_req:reply(200, #{}, Resp, Req), Req0 = cowboy_req:reply(200, #{}, Resp, Req),
{true, Req0, []}; {true, Req0, []};
{error, Reason} -> {error, _} ->
Resp = json:encode(#{<<"error">> => <<"internal server error">>}), Resp = json:encode(#{<<"error">> => <<"internal server error">>}),
Req0 = cowboy_req:reply(500, #{}, Resp, Req), Req0 = cowboy_req:reply(500, #{}, Resp, Req),
{false, Req0, Reason} {false, Req0, State}
end. end.
user_details(Req, State) -> user_details(Req, State) ->