emqx_dashboard_error_code_api.erl 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_dashboard_error_code_api).
  17. -behaviour(minirest_api).
  18. -include_lib("emqx/include/http_api.hrl").
  19. -include("emqx_dashboard.hrl").
  20. -include_lib("typerefl/include/types.hrl").
  21. -include_lib("hocon/include/hoconsc.hrl").
  22. -export([
  23. api_spec/0,
  24. fields/1,
  25. paths/0,
  26. schema/1,
  27. namespace/0
  28. ]).
  29. -export([
  30. error_codes/2,
  31. error_code/2
  32. ]).
  33. namespace() -> "dashboard".
  34. api_spec() ->
  35. emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true, translate_body => true}).
  36. paths() ->
  37. [
  38. "/error_codes",
  39. "/error_codes/:code"
  40. ].
  41. schema("/error_codes") ->
  42. #{
  43. 'operationId' => error_codes,
  44. get => #{
  45. security => [],
  46. description => ?DESC(error_codes),
  47. tags => [<<"Error Codes">>],
  48. responses => #{
  49. 200 => hoconsc:array(hoconsc:ref(?MODULE, error_code))
  50. }
  51. }
  52. };
  53. schema("/error_codes/:code") ->
  54. #{
  55. 'operationId' => error_code,
  56. get => #{
  57. security => [],
  58. description => ?DESC(error_codes),
  59. tags => [<<"Error Codes">>],
  60. parameters => [
  61. {code,
  62. hoconsc:mk(hoconsc:enum(emqx_dashboard_error_code:all()), #{
  63. desc => <<"API Error Codes">>,
  64. in => path,
  65. example => hd(emqx_dashboard_error_code:all())
  66. })}
  67. ],
  68. responses => #{
  69. 200 => hoconsc:ref(?MODULE, error_code)
  70. }
  71. }
  72. }.
  73. fields(error_code) ->
  74. [
  75. {code, hoconsc:mk(string(), #{desc => <<"Code Name">>})},
  76. {description, hoconsc:mk(string(), #{desc => <<"Description">>})}
  77. ].
  78. error_codes(_, _) ->
  79. {200, emqx_dashboard_error_code:list()}.
  80. error_code(_, #{bindings := #{code := Name}}) ->
  81. case emqx_dashboard_error_code:look_up(Name) of
  82. {ok, Code} ->
  83. {200, Code};
  84. {error, not_found} ->
  85. Message = list_to_binary(io_lib:format("Code name ~p not found", [Name])),
  86. {404, ?NOT_FOUND, Message}
  87. end.