Comments (3)
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 ex_aws
is structured - exactly to combat that issue.
Let's look at a fetch_repos
function for an example GitHub API client. In that architecture, instead of having the function perform the actual request, it returns some data encoding the request without executing it. This data, can be later passed to a request/1
or a request!
function that actually perform the request. This allows having both versions of every function (the raising and non-raising one) with minimal additional code - the error handling is contained and separate from the actual logic.
from tesla.
Hi,
Right now all get/post/put/etc.
functions return either Tesla.Env
struct or raise an error.
The issue with returning :ok/:error
tuples is both very hard and trivial.
It's hard because there are many ways the library could behave and many different expectations from its users - there is no simple one-size-fits-all solution. And it's trivial in terms of implementation.
Please take a look at the two following middlewares:
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? :)
from tesla.
Thank for all the feedback!
Creating two version of each function (e.g. get
and get!
) would require making a decision under what conditions the !
should raise and this can vary in different situations. The same can be achieved using trivial middleware (see examples above).
I'm closing this issue for now. It can be reopened if there is any more input.
from tesla.
Related Issues (20)
- Fuck you HOT 2
- G
- Document the need for telemetry in the mix file HOT 3
- Mint proxy credentials HOT 1
- Issues Working with QuotaGuard QGTunnel HOT 3
- [Proposal] Lower the usage of macros HOT 2
- Setting retry delay on runtime. HOT 1
- Telemetry metadata doesn't have data about response in case of error
- Dialyzer failing on Tesla (master) HOT 3
- Tesla Response Stream is not compatible with Tesla Multipart upload
- Jason.Encoder protocol must always be explicitly implemented HOT 1
- Be more explict on `:no_scheme` error reason HOT 1
- When setting the adapter on runtime, mocking does not work
- Logger debug: true does nothing at runtime HOT 2
- Compiler warnings with Elixir v1.17.0-rc.0 HOT 3
- Tesla.Multipart.assert_part_value! doesn't allow valid stream
- Content type should be case insensitive HOT 2
- Tesla.Mock.pdict_get/0 doesn't work inside of Broadway HOT 3
- Test failures and compiler warnings in Elixir 1.16 (httparrot) HOT 6
- Tesla 1.9.0+ is not compatible with Elixir 1.10 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tesla.