Kurisu
Hello forum!
I’m trying to retrieve an updated conn, returned in the first line of a “with” statement, when second line failed. But I’m getting some error that says new_conn variable doesn’t exist. In the “do” block it is fine though.
Let me show you what my code looks like:
def some_action(conn, params) do
with {:ok1, some_result, new_conn} <- some_func(conn, params),
{:ok2, another_result} <- another_func(some_result)
do
new_conn |> redirect(to: some_path(conn, :index)) # new_conn is accessible here
else
{:error1, message} -> render(conn, "some_error_template.html")
# new_conn is not accessible here
{:error2, message} -> render(new_conn, "other_error_template.html")
end
end
Please how do you think I could get new_conn in the “else” block?
I have an idea but I’m wondering if it is not bad to use a function such as Tuple.append/2 to ensure the new_conn is present in the matching tuple…
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
Wrote about how to safely run a globally unique process in an Elixir cluster, and a scary story from the past!
Learn about :global for r...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
idi527
You can return
{:error, conn, message}from your functions.Kurisu
Ok that way I keep the current code, simple and short. I will just writte one more clause for the function that returns {:error2, message} for it takes one more parameter (conn) and just returns it with its own data.
Thank you ^^
ntalfer
I fell into a similar issue and I don’t want to create new functions that return wanted variables in function.
Let’s take this simple snippet:
You can see that the"a" variable is not bound to the else block context, there’s only the “error” one.
If we go with the solution given in the previous comments, we have to introduce a function that returns “a” but you will easily agree that doing so for a such a simple piece of code is stupid:
I found another solution by using process dictionary:
…but I’m not 100% happy with it either.
Does anyone have a more elegant solution?
Thanks!
idi527
If you care about errors, maybe try using a
caseblock?ntalfer
I don’t care about errors.
As in the original question, I would like to get all variables assigned in the first with clauses (before something went wrong) from the else block.
idi527
That means that you do care about errors and should look into using a
caseblock instead ofwith.peerreynders
You don’t.
You probably ran into this:
You’re building values.
So
So you just have to build the value that contains all the information that you need for the error.
The code in the OP would then look something like this:
Kurisu
Hum, very informative … ^^
ntalfer
thanks @peerreynders
based on this thread, I ended by creating a kind of accumulator that gathers all results got during the multiple clauses
this more generic solution looks like:
thanks!
czrpb
i too am a bit surprized the bindings in the
doarent accessable in theelse, given the my interpretation of the purpose of thewith.so .. is @ntalfer solution still the “best” one? the only other way ive found after a bit of research is to implement some sort of railway oriented version, which is certainly non-trivial just to get access to the bindings.