From f45062e5ed529ff37ff5ed2923ef5482431bd10f Mon Sep 17 00:00:00 2001 From: Alexandre Hamez Date: Fri, 15 Nov 2024 16:05:51 +0100 Subject: [PATCH] fix: no more warnings when Poison or Jason are not in the dependencies --- lib/protox/jason.ex | 57 ++++++++++++++++++++++++++++---------------- lib/protox/poison.ex | 57 ++++++++++++++++++++++++++++---------------- 2 files changed, 74 insertions(+), 40 deletions(-) diff --git a/lib/protox/jason.ex b/lib/protox/jason.ex index 7de1b444..5fa379aa 100644 --- a/lib/protox/jason.ex +++ b/lib/protox/jason.ex @@ -2,32 +2,49 @@ defmodule Protox.Jason do @moduledoc false @behaviour Protox.JsonLibrary - @impl true - def load() do - if Code.ensure_loaded?(Jason) do + if Code.ensure_loaded?(Jason) do + @impl true + def load() do {:ok, Jason} - else + end + + @impl true + def decode!(_jason_module, iodata) do + try do + Jason.decode!(iodata) + rescue + e in Jason.DecodeError -> + reraise Protox.JsonDecodingError.new(Exception.message(e)), __STACKTRACE__ + end + end + + @impl true + def encode!(_jason_module, term) do + try do + Jason.encode!(term) + rescue + e in Jason.EncodeError -> + reraise Protox.JsonEncodingError.new(Exception.message(e)), __STACKTRACE__ + end + end + else + @impl true + def load() do :error end - end - @impl true - def decode!(jason_module, iodata) do - try do - jason_module.decode!(iodata) - rescue - e in Jason.DecodeError -> - reraise Protox.JsonDecodingError.new(Exception.message(e)), __STACKTRACE__ + @impl true + def decode!(_jason_module, _iodata) do + raise Protox.JsonDecodingError.new( + "Jason library not loaded. Please check your project dependencies." + ) end - end - @impl true - def encode!(jason_module, term) do - try do - jason_module.encode!(term) - rescue - e in Jason.EncodeError -> - reraise Protox.JsonEncodingError.new(Exception.message(e)), __STACKTRACE__ + @impl true + def encode!(_jason_module, _term) do + raise Protox.JsonEncodingError.new( + "Jason library not loaded. Please check your project dependencies." + ) end end end diff --git a/lib/protox/poison.ex b/lib/protox/poison.ex index 78af1186..c8eb94eb 100644 --- a/lib/protox/poison.ex +++ b/lib/protox/poison.ex @@ -2,32 +2,49 @@ defmodule Protox.Poison do @moduledoc false @behaviour Protox.JsonLibrary - @impl true - def load() do - if Code.ensure_loaded?(Poison) do + if Code.ensure_loaded?(Poison) do + @impl true + def load() do {:ok, Poison} - else + end + + @impl true + def decode!(poison_module, iodata) do + try do + poison_module.decode!(iodata) + rescue + e in [Poison.DecodeError, Poison.ParseError] -> + reraise Protox.JsonDecodingError.new(Exception.message(e)), __STACKTRACE__ + end + end + + @impl true + def encode!(poison_module, term) do + try do + poison_module.encode!(term) + rescue + e in Poison.EncodeError -> + reraise Protox.JsonEncodingError.new(Exception.message(e)), __STACKTRACE__ + end + end + else + @impl true + def load() do :error end - end - @impl true - def decode!(poison_module, iodata) do - try do - poison_module.decode!(iodata) - rescue - e in [Poison.DecodeError, Poison.ParseError] -> - reraise Protox.JsonDecodingError.new(Exception.message(e)), __STACKTRACE__ + @impl true + def decode!(_poison_module, _iodata) do + raise Protox.JsonDecodingError.new( + "Poison library not loaded. Please check your project dependencies." + ) end - end - @impl true - def encode!(poison_module, term) do - try do - poison_module.encode!(term) - rescue - e in Poison.EncodeError -> - reraise Protox.JsonEncodingError.new(Exception.message(e)), __STACKTRACE__ + @impl true + def encode!(_poison_module, _term) do + raise Protox.JsonEncodingError.new( + "Poison library not loaded. Please check your project dependencies." + ) end end end