chanon
I am using something like this to to handle Websocket requests:
def handle_in("new_msg", data, socket) do
# do something with data that might result in exceptions/exits/errors raised
{:reply, {:ok, output}, socket}
end
And in JavaScript side, something like this
channel.push("new_msg", data, 10000)
.receive("ok", (output) => console.log("created message", output) )
.receive("error", (reasons) => console.log("create failed", reasons) )
.receive("timeout", () => console.log("Networking issue...") )
What happens is, when an exception is raised it seems the whole channel process (?) goes down, and the client which is waiting for a response gets no response and just times out.
What I would like instead is to be able to return an error immediately with maybe “unexpected error” reason kind of like a http 500 error.
I don’t want to leave the client hanging and it isn’t a “Networking issue” so timeout isn’t appropriate result.
After some trial and error, I now have the following, which seems to work nicely:
def handle_in("new_msg", data, socket) do
try do
# do something with data that might result in exceptions/exits/errors raised
{:reply, {:ok, output}, socket}
catch
:exit, error ->
Logger.error(Exception.format_exit(reason))
{:reply, {:error, %{reason: "Unexpected Error"}}, socket}
end
end
But I am a bit unsure, as
- I think for http requests Phoenix handles this and automatically responds with 500 code? But for websocket requests I have to handle this myself?
- And also the Elixir tutorial seems to say that you wouldn’t normally need to use try/catch
- And would I also need a rescue?
EDIT: I am now doing it like this as all my messages need replies:
# try catch here
def handle_in(event, params, socket) do
try do
{:reply, handle(event, params, socket), socket}
catch
:exit, reason ->
Logger.info("responding with unexpected error")
Logger.error(Exception.format_exit(reason))
{:reply, {:error, %{reason: "Unexpected Error."}}, socket}
end
end
# handle each message type, no boilerplate
defp handle("new_msg", params, socket) do
# do stuff
{:ok, %{result: output}}
end
defp handle("another_msg", params, socket) do
# do stuff
{:ok, %{result: output}}
end
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security











Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
lud
Hello,
I have the same problem. I use promises to call into the channel from different parts of my javascrtipt code :
But I have no idea how to catch exits from the channel. It would be very useful during development process.
Of course we could handle
channel.onErrorbut you have to handle all errors, not only from the current push.idi527
You can write a custom phoenix channel which would send an error message to the client on exception and keep working. It would probably be similar to the default channel implementation bot with plug’s error handler try/catch logic like here (I think).
Is it a good idea? ¯\(ツ)/¯
lud
I’d rather stick with a JS only solution as the default channel implementation is great but I believe it is not possible because of how websockets work : you can send multiple messages from the browser in a timespan an not be able to known wich one caused the server channel process to exit.
A satisfying solution would be to
.receive('error', fn)when the socket is disconnected while doing a push instead of only receiving a timeout, something like that:But I do not know if
socket.onErrordoes always mean that it’s disconnected or if a push could resolve anyway. Maybe a check toisConnectedwould be necessary with something likemaybeRejectUnsubIfDisconnectedalong withrejectUnsub.idi527
I think there is also
channel.onErrorwhich is supposed to handle channel process dying and socket disconnecting. I’d guesssocket.onErroronly handles the latter.