mirror of
https://github.com/processone/ejabberd.git
synced 2025-01-03 18:02:28 +01:00
Clean-up of error codes and format json structure
This commit is contained in:
parent
d4d1941133
commit
2c70c572c8
@ -220,8 +220,12 @@ process([Call], #request{method = 'POST', data = Data, ip = {IP, _} = IPPort} =
|
|||||||
log(Call, Args, IPPort),
|
log(Call, Args, IPPort),
|
||||||
case check_permissions(Req, Call) of
|
case check_permissions(Req, Call) of
|
||||||
{allowed, Cmd, Auth} ->
|
{allowed, Cmd, Auth} ->
|
||||||
{Code, Result} = handle(Cmd, Auth, Args, Version, IP),
|
case handle(Cmd, Auth, Args, Version, IP) of
|
||||||
json_response(Code, jiffy:encode(Result));
|
{Code, Result} ->
|
||||||
|
json_response(Code, jiffy:encode(Result));
|
||||||
|
{HTMLCode, JSONErrorCode, Message} ->
|
||||||
|
json_error(HTMLCode, JSONErrorCode, Message)
|
||||||
|
end;
|
||||||
%% Warning: check_permission direcly formats 401 reply if not authorized
|
%% Warning: check_permission direcly formats 401 reply if not authorized
|
||||||
ErrorResponse ->
|
ErrorResponse ->
|
||||||
ErrorResponse
|
ErrorResponse
|
||||||
@ -278,6 +282,8 @@ get_api_version([]) ->
|
|||||||
%% command handlers
|
%% command handlers
|
||||||
%% ----------------
|
%% ----------------
|
||||||
|
|
||||||
|
%% TODO Check accept types of request before decided format of reply.
|
||||||
|
|
||||||
% generic ejabberd command handler
|
% generic ejabberd command handler
|
||||||
handle(Call, Auth, Args, Version, IP) when is_atom(Call), is_list(Args) ->
|
handle(Call, Auth, Args, Version, IP) when is_atom(Call), is_list(Args) ->
|
||||||
case ejabberd_commands:get_command_format(Call, Auth, Version) of
|
case ejabberd_commands:get_command_format(Call, Auth, Version) of
|
||||||
@ -309,8 +315,8 @@ handle(Call, Auth, Args, Version, IP) when is_atom(Call), is_list(Args) ->
|
|||||||
{401, jlib:atom_to_binary(Why)};
|
{401, jlib:atom_to_binary(Why)};
|
||||||
throw:{not_allowed, Msg} ->
|
throw:{not_allowed, Msg} ->
|
||||||
{401, iolist_to_binary(Msg)};
|
{401, iolist_to_binary(Msg)};
|
||||||
throw:{error, account_unprivileged} ->
|
throw:{error, account_unprivileged} ->
|
||||||
{401, iolist_to_binary(<<"Unauthorized: Account Unpriviledged">>)};
|
{403, 31, <<"Command need to be run with admin priviledge.">>};
|
||||||
throw:{invalid_parameter, Msg} ->
|
throw:{invalid_parameter, Msg} ->
|
||||||
{400, iolist_to_binary(Msg)};
|
{400, iolist_to_binary(Msg)};
|
||||||
throw:{error, Why} when is_atom(Why) ->
|
throw:{error, Why} when is_atom(Why) ->
|
||||||
@ -490,9 +496,7 @@ format_result(404, {_Name, _}) ->
|
|||||||
"not_found".
|
"not_found".
|
||||||
|
|
||||||
unauthorized_response() ->
|
unauthorized_response() ->
|
||||||
unauthorized_response(<<"401 Unauthorized">>).
|
json_error(401, 10, <<"Oauth Token is invalid or expired.">>).
|
||||||
unauthorized_response(Body) ->
|
|
||||||
json_response(401, jiffy:encode(Body)).
|
|
||||||
|
|
||||||
badrequest_response() ->
|
badrequest_response() ->
|
||||||
badrequest_response(<<"400 Bad Request">>).
|
badrequest_response(<<"400 Bad Request">>).
|
||||||
@ -502,6 +506,15 @@ badrequest_response(Body) ->
|
|||||||
json_response(Code, Body) when is_integer(Code) ->
|
json_response(Code, Body) when is_integer(Code) ->
|
||||||
{Code, ?HEADER(?CT_JSON), Body}.
|
{Code, ?HEADER(?CT_JSON), Body}.
|
||||||
|
|
||||||
|
%% HTTPCode, JSONCode = integers
|
||||||
|
%% message is binary
|
||||||
|
json_error(HTTPCode, JSONCode, Message) ->
|
||||||
|
{HTTPCode, ?HEADER(?CT_JSON),
|
||||||
|
jiffy:encode({[{<<"status">>, <<"error">>},
|
||||||
|
{<<"code">>, JSONCode},
|
||||||
|
{<<"message">>, Message}]})
|
||||||
|
}.
|
||||||
|
|
||||||
log(Call, Args, {Addr, Port}) ->
|
log(Call, Args, {Addr, Port}) ->
|
||||||
AddrS = jlib:ip_to_list({Addr, Port}),
|
AddrS = jlib:ip_to_list({Addr, Port}),
|
||||||
?INFO_MSG("API call ~s ~p from ~s:~p", [Call, Args, AddrS, Port]);
|
?INFO_MSG("API call ~s ~p from ~s:~p", [Call, Args, AddrS, Port]);
|
||||||
|
Loading…
Reference in New Issue
Block a user