Skip to content

Commit

Permalink
KAZOO-3402: handle error converting to binary for csv downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
k-anderson committed Mar 12, 2015
1 parent c281aea commit a885c41
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
10 changes: 9 additions & 1 deletion applications/crossbar/src/api_resource.erl
Original file line number Diff line number Diff line change
Expand Up @@ -800,10 +800,18 @@ csv_header(JObj) ->
-spec csv_ize(wh_json:keys()) -> iolist().
csv_ize([F|Rest]) ->
[<<"\"">>, wh_util:to_binary(F), <<"\"">>
,[[<<",\"">>, wh_util:to_binary(V), <<"\"">>] || V <- Rest]
,[[<<",\"">>, try_to_binary(V), <<"\"">>] || V <- Rest]
,<<"\n">>
].

-spec try_to_binary(_) -> binary().
try_to_binary(Value) ->
try wh_util:to_binary(Value) of
V -> V
catch
_E:_R -> <<"">>
end.

-spec json_to_csv(wh_json:object()) -> iolist().
json_to_csv(JObj) ->
{Vs, _} = wh_json:get_values(correct_jobj(JObj)),
Expand Down
17 changes: 16 additions & 1 deletion applications/crossbar/src/modules/cb_transactions.erl
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,22 @@ fetch(Context, Options) ->
{'error', _R}=Error -> send_resp(Error, Context);
{'ok', Transactions} ->
JObjs = maybe_filter_by_reason(Transactions, Options),
send_resp({'ok', wht_util:collapse_call_transactions(JObjs)}, Context)
send_resp(flatten(JObjs, []), Context)
end.

-spec flatten(wh_json:objects(), wh_json:objects()) -> {'ok', wh_json:objects()}.
flatten([], Results) ->
{'ok', wht_util:collapse_call_transactions(Results)};
flatten([JObj|JObjs], Results) ->
Metadata = wh_json:get_ne_value(<<"metadata">>, JObj),
case wh_json:is_json_object(Metadata) of
'true' ->
Props = wh_json:to_proplist(
wh_json:delete_key(<<"account_id">>, Metadata)
),
flatten(JObjs, [wh_json:set_values(Props, JObj)|Results]);
'false' ->
flatten(JObjs, [JObj|Results])
end.

%%--------------------------------------------------------------------
Expand Down

0 comments on commit a885c41

Please sign in to comment.