impl callback map_error() (#476)

This commit is contained in:
Boris 2020-05-13 13:55:24 +03:00 committed by GitHub
parent b704db562d
commit 0268dbeba4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -19,7 +19,7 @@ SERVICE_IMAGE_PUSH_TAG ?= $(SERVICE_IMAGE_TAG)
BASE_IMAGE_NAME := service-erlang
BASE_IMAGE_TAG := da0ab769f01b650b389d18fc85e7418e727cbe96
BUILD_IMAGE_TAG := 442c2c274c1d8e484e5213089906a4271641d95e
BUILD_IMAGE_TAG := 0c638a682f4735a65ef232b81ed872ba494574c3
CALL_ANYWHERE := \
submodules \

View File

@ -2,8 +2,11 @@
-behaviour(swag_server_logic_handler).
-type error_type() :: swag_server_logic_handler:error_type().
%% API callbacks
-export([authorize_api_key/3]).
-export([map_error/2]).
-export([handle_request/4]).
%% Handler behaviour
@ -39,6 +42,26 @@ authorize_api_key(OperationID, ApiKey, _HandlerOpts) ->
false
end.
-spec map_error(error_type(), swag_server_validation:error()) ->
swag_server:error_reason().
map_error(validation_error, Error) ->
Type = genlib:to_binary(maps:get(type, Error)),
Name = genlib:to_binary(maps:get(param_name, Error)),
Message = case maps:get(description, Error, undefined) of
undefined ->
<<"Request parameter: ", Name/binary, ", error type: ", Type/binary>>;
Description ->
DescriptionBin = genlib:to_binary(Description),
<<"Request parameter: ", Name/binary,
", error type: ", Type/binary,
", description: ", DescriptionBin/binary>>
end,
jsx:encode(#{
<<"code">> => <<"invalidRequest">>,
<<"message">> => Message
}).
-type request_data() :: #{atom() | binary() => term()}.
-type operation_id() :: swag_server:operation_id().