From 441a9ddcb568183614530a5f5878c0525f20c58e Mon Sep 17 00:00:00 2001 From: Geoff Lee Date: Mon, 25 Sep 2017 13:51:00 -0700 Subject: [PATCH] Provide more descriptive error message when JSX fails to decode ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ On branch aws-support - Mon 25 Sep 2017 13:51:00 PDT by Geoff Lee --- lib/tirexs/http.ex | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/tirexs/http.ex b/lib/tirexs/http.ex index ccbc6ad..8de3880 100644 --- a/lib/tirexs/http.ex +++ b/lib/tirexs/http.ex @@ -335,7 +335,13 @@ defmodule Tirexs.HTTP do @doc false def decode(json, opts \\ [{:labels, :atom}]) do - JSX.decode!(IO.iodata_to_binary(json), opts) + with binary <- IO.iodata_to_binary(json), + {:ok, decoded_json} <- JSX.decode(binary, opts) do + decoded_json + else + {:error, msg} -> + raise "Response is invalid JSON. Response: \"#{json}\". JSX Error: \"#{msg}\"" + end end defp __merge__(map1, map2) do