From 20665f725c19953f4453d35a73f72db27976a073 Mon Sep 17 00:00:00 2001 From: "Paulo F. Oliveira" <paulo.oliveira@kivra.com> Date: Thu, 9 Nov 2023 13:39:33 +0000 Subject: [PATCH] Allow (visually) exporting the list of known API error codes The goal, as stated in the change, is to easy copy-pasting to developer.kivra.com, thus reducing the chance of inconsistency --- README.md | 22 ++++++++++++++++++++++ erlang/kivra_api_errors.erl | 23 ++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ce1ceae..f0010ec 100644 --- a/README.md +++ b/README.md @@ -127,3 +127,25 @@ The following message contains the same information, but is more concise: Run the linter to validate the error definitions in [`api-errors.json`](./api-errors.json): make lint + +## Export for use in `developer.kivra.com` + +A helper function is made available to allow copy-pasting the entire list for +acceptance in <https://developer.kivra.com>. + +### Erlang + +Boot up an Erlang shell + +```console +rebar3 shell +``` + +Call the `kivra_api_errors:to_developer_kivra_com/0` function: + +```erlang +kivra_api_errors:to_developer_kivra_com(). +``` + +Whatever is between `<copy-paste>` and `</copy-paste>` is deemed usable +(YML-formatted) in <https://developer.kivra.com>. diff --git a/erlang/kivra_api_errors.erl b/erlang/kivra_api_errors.erl index f298763..bf662dd 100644 --- a/erlang/kivra_api_errors.erl +++ b/erlang/kivra_api_errors.erl @@ -5,7 +5,8 @@ -export([ load/0, load/1, - from_code/1, from_code/2, from_code/3 + from_code/1, from_code/2, from_code/3, + to_developer_kivra_com/0 ]). %%%_* Types =================================================================== @@ -37,6 +38,26 @@ load(Config) -> Error end. +-spec to_developer_kivra_com() -> ok. +to_developer_kivra_com() -> + ErrorsFile = filename:join([code:priv_dir(?MODULE), <<"api-errors.json">>]), + {ok, ErrorsAsJSON} = file:read_file(ErrorsFile), + Errors = jiffy:decode(ErrorsAsJSON, [return_maps]), + ErrorsList = maps:to_list(Errors), + SortedErrorsList = lists:sort(fun ({CodeLeft, _}, {CodeRight, _}) -> CodeLeft < CodeRight end, ErrorsList), + + io:format("Copy-paste to developer.kivra.com's swagger.yml~n"), + io:format("<copy-paste>~n"), + io:format(" | Code | Short Message | Long Message |~n"), + io:format(" | ---- | ------------- | ------------ |~n"), + lists:foreach( + fun ({Code, #{<<"short_message">> := ShortMessage, <<"long_message">> := LongMessage}}) -> + io:format(" | ~ts | ~ts | ~ts |~n", [Code, ShortMessage, LongMessage]) + end, + SortedErrorsList + ), + io:format("</copy-paste>~n"). + -spec from_code(binary() | pos_integer()) -> {ok, {status_code(), payload_map() | payload_kv()}} | {error, notfound}. from_code(ErrorCode) when is_integer(ErrorCode) -> from_code(integer_to_binary(ErrorCode));