Skip to content

Commit

Permalink
Allow (visually) exporting the list of known API error codes
Browse files Browse the repository at this point in the history
The goal, as stated in the change, is to easy copy-pasting
to developer.kivra.com, thus reducing the chance of
inconsistency
  • Loading branch information
kivra-pauoli committed Nov 9, 2023
1 parent 29dbfe4 commit 20665f7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>.
23 changes: 22 additions & 1 deletion erlang/kivra_api_errors.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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 ===================================================================
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 20665f7

Please sign in to comment.