Skip to content

Commit

Permalink
consider accepted and success as "successful" resp_status (2600hz#5508)
Browse files Browse the repository at this point in the history
If an endpoint (like cb_channels) uses crossbar_util:response_202/3
during validation, api_util will consider validation unsuccessful and
not continue to the 'execute' phase.
  • Loading branch information
jamesaimonetti authored and lazedo committed Feb 12, 2019
1 parent 348fe97 commit 62d202b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
12 changes: 6 additions & 6 deletions applications/crossbar/src/api_resource.erl
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,9 @@ malformed_request(Req, Context, _ReqVerb) ->
{'false', Req, Context};
[?MATCH_ACCOUNT_RAW(_) | _] = AccountArgs ->
Context1 = validate_account_resource(Context, AccountArgs),
case cb_context:resp_status(Context1) of
'success' -> {'false', Req, Context1};
_RespStatus -> api_util:stop(Req, Context1)
case api_util:succeeded(Context1) of
'true' -> {'false', Req, Context1};
'false' -> api_util:stop(Req, Context1)
end;
[<<>> | _] ->
Error = kz_json:from_list([{<<"message">>, <<"missing account_id">>}]),
Expand Down Expand Up @@ -1113,7 +1113,7 @@ reset_context_between_chunks(Context, StartedChunk) ->
,{fun cb_context:store/3, 'chunking_started', StartedChunk}
]
),
reset_context_between_chunks(Context1, StartedChunk, cb_context:resp_status(Context)).
reset_context_between_chunks(Context1, StartedChunk, api_util:succeeded(Context)).

%%------------------------------------------------------------------------------
%% @doc Reset response data to an empty or check an error message is set.
Expand All @@ -1122,9 +1122,9 @@ reset_context_between_chunks(Context, StartedChunk) ->
%% and if not set it to an empty list.
%% @end
%%------------------------------------------------------------------------------
reset_context_between_chunks(Context, _StartedChunk, 'success') ->
reset_context_between_chunks(Context, _StartedChunk, 'true') ->
cb_context:set_resp_data(Context, []);
reset_context_between_chunks(Context, _StartedChunk, _) ->
reset_context_between_chunks(Context, _StartedChunk, 'false') ->
RespData = cb_context:resp_data(Context),
case {kz_json:is_json_object(RespData)
,kz_term:is_ne_binary(RespData)
Expand Down
20 changes: 10 additions & 10 deletions applications/crossbar/src/api_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,13 @@ process_billing_response(Context, NewContext) ->
%% @end
%%------------------------------------------------------------------------------
-spec succeeded(cb_context:context()) -> boolean().
succeeded(Context) -> cb_context:resp_status(Context) =:= 'success'.
succeeded(Context) ->
is_successful_status(cb_context:resp_status(Context)).

-spec is_successful_status(crossbar_status()) -> boolean().
is_successful_status('success') -> 'true';
is_successful_status('accepted') -> 'true';
is_successful_status(_Status) -> 'false'.

-spec execute_request(cowboy_req:req(), cb_context:context()) ->
{boolean() | 'stop', cowboy_req:req(), cb_context:context()}.
Expand Down Expand Up @@ -1146,18 +1152,12 @@ execute_request_failure(Req, Context, _E) ->
lager:debug("unexpected return from the fold: ~p", [_E]),
{'false', Req, Context}.

-spec execute_request_results(cowboy_req:req(), cb_context:context()) ->
{'true' | 'stop', cowboy_req:req(), cb_context:context()}.
execute_request_results(Req, Context) ->
case succeeded(Context) of
'false' -> ?MODULE:stop(Req, Context);
'true' -> {'true', Req, Context}
end.

-spec execute_request_results(cowboy_req:req(), cb_context:context(), crossbar_status()) ->
{'true' | 'stop', cowboy_req:req(), cb_context:context()}.
execute_request_results(Req, Context, 'success') ->
execute_request_results(Req, Context);
{'true', Req, Context};
execute_request_results(Req, Context, 'accepted') ->
{'true', Req, Context};
execute_request_results(Req, Context, _RespStatus) ->
?MODULE:stop(Req, Context).

Expand Down
4 changes: 3 additions & 1 deletion applications/crossbar/src/crossbar_types.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
-define(HARD_DELETE, 'false').
-define(SOFT_DELETE, 'true').

-type crossbar_status() :: 'success' | 'accepted' | 'error' | 'fatal' | 'stop'.
-type successful_status() :: 'success' | 'accepted'.
-type error_status() :: 'error' | 'fatal' | 'stop'.
-type crossbar_status() :: successful_status() | error_status().
-type crossbar_module_result() :: {crossbar_status(), kz_term:proplist()} |
{crossbar_status(), kz_term:proplist(), string()} |
{crossbar_status(), kz_term:proplist(), string(), integer()}.
Expand Down

0 comments on commit 62d202b

Please sign in to comment.