Packages
honeybadger
0.26.0
0.28.0
0.27.0
0.26.0
0.25.0
0.24.1
0.24.0
0.23.0
0.22.1
0.22.0
0.21.0
0.20.0
0.19.0
0.18.1
0.18.0
0.17.0
0.16.4
0.16.3
0.16.2
0.16.1
0.16.0
0.15.0
0.14.1
0.14.0
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.10.3
0.10.2
0.10.1
0.10.0
0.9.0
0.8.0
0.7.0
0.7.0-beta
0.6.3
0.6.2
0.6.1
0.6.0
0.5.0
0.4.0
0.3.1
0.3.0
0.2.0
0.1.2
0.1.1
0.1.0
Elixir client, Plug and error_logger for integrating with the Honeybadger.io exception tracker"
Current section
Files
Jump to
Current section
Files
lib/honeybadger/json.ex
defmodule Honeybadger.JSON do
@moduledoc false
@spec encode(term) ::
{:ok, String.t()}
| {:error, Jason.EncodeError.t()}
| {:error, Exception.t()}
def encode(term) do
case safe_encode(term) do
{:ok, output} ->
{:ok, output}
{:error, _error} ->
term
|> to_encodeable()
|> Jason.encode()
end
end
# Keep from converting DateTime to a map
defp to_encodeable(%DateTime{} = datetime), do: datetime
# struct
defp to_encodeable(%_{} = struct) do
struct
|> Map.from_struct()
|> to_encodeable()
end
# map
defp to_encodeable(map) when is_map(map) do
for {key, val} <- map, into: %{} do
{key, to_encodeable(val)}
end
end
# list
defp to_encodeable(list) when is_list(list) do
for element <- list, do: to_encodeable(element)
end
# tuple
defp to_encodeable(tuple) when is_tuple(tuple) do
tuple
|> Tuple.to_list()
|> to_encodeable
end
defp to_encodeable(input)
when is_pid(input) or is_port(input) or is_reference(input) or is_function(input) do
inspect(input)
end
defp to_encodeable(input) when is_binary(input) do
case :unicode.characters_to_binary(input) do
{:error, binary, _rest} -> binary
{:incomplete, binary, _rest} -> binary
_ -> input
end
end
defp to_encodeable(input) do
input
end
def safe_encode(input) do
Jason.encode(input)
rescue
error in Protocol.UndefinedError ->
{:error, error}
end
end