I was about to create an issue on the Elixir repo about the current behaviour of Base.decodeXX
functions in core. I’ll first post it here and if people agree I’ll open the issue and send a PR. Just wanted to avoid polluting the issue tracker and getting some feedback beforehand.
Issue description
Base.decodeXX
functions return a very unuseful :error
only atom. This is fine for all the cases that you trust the encoding. Sometimes, though, we need to define some external “protocol” that will send/receive encoded binaries. In these cases, it is common to have something like:
def some_input_handling_fun(binary) do
with {:ok, decoded} <- Base.decode64(binary),
{:ok, result} <- do_something_with_decoded(decoded) do
{:ok, result}
end
end
The issue is that the return of decodeXX
funs, if it fails, is simply :error
which makes debugging this a bit tricky. If anything else returns :error
(which it shouldn’t but you know how mankind works, right?) then we will need more context to understand what went wrong.
If this is ok, I can send a PR changing these specs and implementation here, here and here.
This is public API and so, I am not sure if this can be done as it would be backwards incompatible. I think this is a bug since many times it was mentioned that bad error “practices” in the core language should be treated as such. If this is not the case, then I’ll wrap the function locally anyway
Environment
- Elixir & Erlang/OTP versions (elixir --version):
elixir --version
Erlang/OTP 21 [erts-10.0.6] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]
Elixir 1.7.3 (compiled with Erlang/OTP 21)
- Operating system:
uname -a
Linux 4.17.19-200.fc28.x86_64 #1 SMP Fri Aug 24 15:47:41 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
Current behavior
Not following error tuples for decoding operations.
Expected behavior
Return {:error, :bad_encoding}
instead of just :error