Skip to content

Commit 430bff2

Browse files
committed
configurable JSON libraries
1 parent 7ba2a3e commit 430bff2

File tree

8 files changed

+50
-10
lines changed

8 files changed

+50
-10
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ notifications:
1919
script:
2020
- if [ "$STRICT" = "true" ]; then mix compile --warnings-as-errors; fi
2121
- mix test
22-
- mix credo
22+
- MIX_ENV=test mix credo
2323
- if [ "$STRICT" = "true" ]; then travis_wait mix dialyzer; fi
2424
- if [ "$STRICT" = "true" ]; then mix format --dry-run --check-formatted; fi
2525
cache:

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## 7.0.0 (2018-09-05)
66

77
* Enhancements
8-
* Replace Poison with Jason
8+
* Replace Poison with configurable JSON library
99
* Implement `Sentry.LoggerBackend`
1010

1111
* Breaking Changes

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ If you would like to upgrade a project to use Sentry 7.x, see [here](https://gis
1717

1818
## Installation
1919

20-
To use Sentry with your projects, edit your mix.exs file and add it as a dependency:
20+
To use Sentry with your projects, edit your mix.exs file and add it as a dependency. Sentry defaults to trying to use Jason for JSON operations, but can be configured to use other ones.
2121

2222
```elixir
2323
defp deps do
2424
[
2525
# ...
2626
{:sentry, "~> 7.0"},
27+
{:jason, "~> 1.1"},
2728
]
2829
end
2930
```
@@ -116,6 +117,7 @@ For optional settings check the [docs](https://hexdocs.pm/sentry/readme.html).
116117
| `source_code_exclude_patterns` | False | `[~r"/_build/", ~r"/deps/", ~r"/priv/"]` | |
117118
| `source_code_path_pattern` | False | `"**/*.ex"` | |
118119
| `filter` | False | | Module where the filter rules are defined (see [Filtering Exceptions](https://hexdocs.pm/sentry/Sentry.html#module-filtering-exceptions)) |
120+
| `json_library` | False | `Jason` | |
119121

120122
An example production config might look like this:
121123

lib/sentry.ex

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ defmodule Sentry do
103103
)
104104
]
105105

106-
opts = [strategy: :one_for_one, name: Sentry.Supervisor]
106+
validate_json_config!()
107107

108+
opts = [strategy: :one_for_one, name: Sentry.Supervisor]
108109
Supervisor.start_link(children, opts)
109110
end
110111

@@ -164,4 +165,31 @@ defmodule Sentry do
164165
:ignored
165166
end
166167
end
168+
169+
defp validate_json_config!() do
170+
case Config.json_library() do
171+
nil ->
172+
raise ArgumentError.exception("nil is not a valid :json_library configuration")
173+
174+
library ->
175+
try do
176+
with {:ok, %{}} <- library.decode("{}"),
177+
{:ok, "{}"} <- library.encode(%{}) do
178+
:ok
179+
else
180+
_ ->
181+
raise ArgumentError.exception(
182+
"configured :json_library #{inspect(library)} does not implement decode/1 and encode/1"
183+
)
184+
end
185+
rescue
186+
UndefinedFunctionError ->
187+
reraise ArgumentError.exception("""
188+
configured :json_library #{inspect(library)} is not available or does not implement decode/1 and encode/1.
189+
Do you need to add #{inspect(library)} to your mix.exs?
190+
"""),
191+
__STACKTRACE__
192+
end
193+
end
194+
end
167195
end

lib/sentry/client.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ defmodule Sentry.Client do
7474
end
7575

7676
defp encode_and_send(event, result) do
77+
json_library = Config.json_library()
78+
7779
render_event(event)
78-
|> Jason.encode()
80+
|> json_library.encode()
7981
|> case do
8082
{:ok, body} ->
8183
do_send_event(event, body, result)
@@ -158,13 +160,15 @@ defmodule Sentry.Client do
158160
@spec request(String.t(), list({String.t(), String.t()}), String.t()) ::
159161
{:ok, String.t()} | {:error, term()}
160162
def request(url, headers, body) do
163+
json_library = Config.json_library()
164+
161165
hackney_opts =
162166
Config.hackney_opts()
163167
|> Keyword.put_new(:pool, @hackney_pool_name)
164168

165169
with {:ok, 200, _, client} <- :hackney.request(:post, url, headers, body, hackney_opts),
166170
{:ok, body} <- :hackney.body(client),
167-
{:ok, json} <- Jason.decode(body) do
171+
{:ok, json} <- json_library.decode(body) do
168172
{:ok, Map.get(json, "id")}
169173
else
170174
{:ok, status, headers, client} ->

lib/sentry/config.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ defmodule Sentry.Config do
123123
get_config(:report_deps, default: true, check_dsn: false)
124124
end
125125

126+
def json_library do
127+
get_config(:json_library, default: Jason, check_dsn: false)
128+
end
129+
126130
defp get_config(key, opts \\ []) when is_atom(key) do
127131
default = Keyword.get(opts, :default)
128132
check_dsn = Keyword.get(opts, :check_dsn, true)

lib/sentry/plug.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
if Code.ensure_loaded?(Plug) do
2-
require Protocol
3-
Protocol.derive(Jason.Encoder, Plug.Upload)
2+
if Code.ensure_loaded?(Jason) do
3+
require Protocol
4+
Protocol.derive(Jason.Encoder, Plug.Upload)
5+
end
46

57
defmodule Sentry.Plug do
68
@default_scrubbed_param_keys ["password", "passwd", "secret"]

mix.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ defmodule Sentry.Mixfile do
2525
defp deps do
2626
[
2727
{:hackney, "~> 1.8 or 1.6.5"},
28-
{:jason, "~> 1.1"},
28+
{:jason, "~> 1.1", only: [:test]},
2929
{:plug, "~> 1.6", optional: true},
3030
{:phoenix, "~> 1.3", optional: true},
3131
{:dialyxir, "> 0.0.0", only: [:dev], runtime: false},
3232
{:ex_doc, "~> 0.19.0", only: :dev},
33-
{:credo, "~> 0.10.0", only: [:dev, :test], runtime: false},
33+
{:credo, "~> 0.10.0", only: [:test], runtime: false},
3434
{:bypass, "~> 0.8.0", only: [:test]}
3535
]
3636
end

0 commit comments

Comments
 (0)