diff --git a/apps/emqx_schema_registry/src/emqx_schema_registry.erl b/apps/emqx_schema_registry/src/emqx_schema_registry.erl index 5005b0dc8..6d894ab90 100644 --- a/apps/emqx_schema_registry/src/emqx_schema_registry.erl +++ b/apps/emqx_schema_registry/src/emqx_schema_registry.erl @@ -77,7 +77,7 @@ is_existing_type(SchemaName, Path) -> -spec get_schema(schema_name()) -> {ok, map()} | {error, not_found}. get_schema(SchemaName) -> - case + try emqx_config:get( [?CONF_KEY_ROOT, schemas, schema_name_bin_to_atom(SchemaName)], undefined ) @@ -86,6 +86,9 @@ get_schema(SchemaName) -> {error, not_found}; Config -> {ok, Config} + catch + throw:not_found -> + {error, not_found} end. -spec add_schema(schema_name(), schema()) -> ok | {error, term()}. @@ -340,7 +343,7 @@ to_bin(A) when is_atom(A) -> atom_to_binary(A); to_bin(B) when is_binary(B) -> B. schema_name_bin_to_atom(Bin) when size(Bin) > 255 -> - erlang:throw( + throw( iolist_to_binary( io_lib:format( "Name is is too long." @@ -351,4 +354,9 @@ schema_name_bin_to_atom(Bin) when size(Bin) > 255 -> ) ); schema_name_bin_to_atom(Bin) -> - binary_to_atom(Bin, utf8). + try + binary_to_existing_atom(Bin, utf8) + catch + error:badarg -> + throw(not_found) + end. diff --git a/apps/emqx_schema_registry/test/emqx_schema_registry_http_api_SUITE.erl b/apps/emqx_schema_registry/test/emqx_schema_registry_http_api_SUITE.erl index dfba3d1cd..2ab05a7e8 100644 --- a/apps/emqx_schema_registry/test/emqx_schema_registry_http_api_SUITE.erl +++ b/apps/emqx_schema_registry/test/emqx_schema_registry_http_api_SUITE.erl @@ -193,6 +193,13 @@ t_crud(Config) -> %% no schemas at first ?assertMatch({ok, 200, []}, request(get)), + ?assertMatch( + {ok, 404, #{ + <<"code">> := <<"NOT_FOUND">>, + <<"message">> := <<"Schema not found">> + }}, + request({get, <<"some_name_that_is_not_an_atom_yet">>}) + ), ?assertMatch( {ok, 404, #{ <<"code">> := <<"NOT_FOUND">>,