From 7d312a630b1d7a4a6ae23e7900a8a005d06beebe Mon Sep 17 00:00:00 2001 From: zhouzb Date: Wed, 8 Sep 2021 16:35:54 +0800 Subject: [PATCH] chore(authn): provide easy-to-read hints for more errors --- apps/emqx/src/emqx_authentication.erl | 4 +-- apps/emqx_authn/src/emqx_authn_api.erl | 38 +++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/apps/emqx/src/emqx_authentication.erl b/apps/emqx/src/emqx_authentication.erl index 8cc8cf2df..b9973033c 100644 --- a/apps/emqx/src/emqx_authentication.erl +++ b/apps/emqx/src/emqx_authentication.erl @@ -484,7 +484,7 @@ handle_call({update_authenticator, ChainName, AuthenticatorID, Config}, _From, S {error, Reason} end; false -> - {error, mechanism_or_backend_change_is_not_alloed} + {error, change_of_authentication_type_is_not_allowed} end end end, @@ -679,7 +679,7 @@ call_authenticator(ChainName, AuthenticatorID, Func, Args) -> true -> erlang:apply(Provider, Func, Args ++ [State]); false -> - {error, unsupported_feature} + {error, unsupported_operation} end end end, diff --git a/apps/emqx_authn/src/emqx_authn_api.erl b/apps/emqx_authn/src/emqx_authn_api.erl index 5ba1419f0..02f3060db 100644 --- a/apps/emqx_authn/src/emqx_authn_api.erl +++ b/apps/emqx_authn/src/emqx_authn_api.erl @@ -1857,8 +1857,7 @@ list_authenticator(ConfKeyPath, AuthenticatorID) -> update_authenticator(ConfKeyPath, ChainName0, AuthenticatorID, Config) -> ChainName = to_atom(ChainName0), - case update_config(ConfKeyPath, - {update_authenticator, ChainName, AuthenticatorID, Config}) of + case update_config(ConfKeyPath, {update_authenticator, ChainName, AuthenticatorID, Config}) of {ok, #{post_config_update := #{?AUTHN := #{id := ID}}, raw_config := AuthenticatorsConfig}} -> {ok, AuthenticatorConfig} = find_config(ID, AuthenticatorsConfig), @@ -1963,25 +1962,56 @@ fill_defaults(Config) -> serialize_error({not_found, {authenticator, ID}}) -> {404, #{code => <<"NOT_FOUND">>, - message => list_to_binary(io_lib:format("Authenticator '~s' does not exist", [ID]))}}; + message => list_to_binary( + io_lib:format("Authenticator '~s' does not exist", [ID]) + )}}; + serialize_error({not_found, {listener, ID}}) -> {404, #{code => <<"NOT_FOUND">>, - message => list_to_binary(io_lib:format("Listener '~s' does not exist", [ID]))}}; + message => list_to_binary( + io_lib:format("Listener '~s' does not exist", [ID]) + )}}; + +serialize_error({not_found, {chain, ?GLOBAL}}) -> + {500, #{code => <<"INTERNAL_SERVER_ERROR">>, + message => <<"Authentication status is abnormal">>}}; + +serialize_error({not_found, {chain, Name}}) -> + {400, #{code => <<"BAD_REQUEST">>, + message => list_to_binary( + io_lib:format("No authentication has been create for listener '~s'", [Name]) + )}}; + serialize_error({already_exists, {authenticator, ID}}) -> {409, #{code => <<"ALREADY_EXISTS">>, message => list_to_binary( io_lib:format("Authenticator '~s' already exist", [ID]) )}}; + +serialize_error(no_available_provider) -> + {400, #{code => <<"BAD_REQUEST">>, + message => <<"Unsupported authentication type">>}}; + +serialize_error(change_of_authentication_type_is_not_allowed) -> + {400, #{code => <<"BAD_REQUEST">>, + message => <<"Change of authentication type is not allowed">>}}; + +serialize_error(unsupported_operation) -> + {400, #{code => <<"BAD_REQUEST">>, + message => <<"Operation not supported in this authentication type">>}}; + serialize_error({missing_parameter, Name}) -> {400, #{code => <<"MISSING_PARAMETER">>, message => list_to_binary( io_lib:format("The input parameter '~p' that is mandatory for processing this request is not supplied", [Name]) )}}; + serialize_error({invalid_parameter, Name}) -> {400, #{code => <<"INVALID_PARAMETER">>, message => list_to_binary( io_lib:format("The value of input parameter '~p' is invalid", [Name]) )}}; + serialize_error(Reason) -> {400, #{code => <<"BAD_REQUEST">>, message => list_to_binary(io_lib:format("~p", [Reason]))}}.