-
Notifications
You must be signed in to change notification settings - Fork 342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More conventional output #64
Comments
Hi, Right now all The issue with returning defmodule Tesla.Middleware.TupleForGoodResponses do
@moduledoc """
Return :ok/:error tuples for "good" responses, i.e. when status code is in the range (200 - 399)
"""
def call(env, next, _opts) do
case Tesla.run(env, next) do
%{status: status} = env when status < 400
-> {:ok, env}
env
-> {:error, env}
end
end
end defmodule Tesla.Middleware.TupleForSuccessfulTransaction do
@moduledoc """
Return :ok/:error tuples for successful HTTP transations, i.e. when the request is completed
(no network errors etc) - but it can still be an application-level error (i.e. 404 or 500)
"""
def call(env, next, _opts) do
try do
{:ok, Tesla.run(env, next)}
rescue
%Tesla.Error{} = er ->
{:error, er}
end
end
end As you can see, implementing both scenarios is pretty easy (and we should probably include some kind of these middlewares but definitely with better names). And since the topic of return values has been brought up here, @michalmuskala got a minute? :) |
Sorry for the late answer. An ideal situation would be to have both a raising and a tuple-return version of each function. But, if there are a lot of functions, that's not really sustainable. I really like how the new Let's look at a |
Thank for all the feedback! Creating two version of each function (e.g. I'm closing this issue for now. It can be reopened if there is any more input. |
Hello, thanks for this great library.
It seems to me like there's some convention in elixir to make normal function return output as tuple
{:ok, value}
or{:error, error}
and for function with ! to return only value or raise error.Would it be difficult to make Tesla work like this?
The text was updated successfully, but these errors were encountered: