Azolo / websockex

An Elixir Websocket Client
MIT License
519 stars 100 forks source link

send_frame should not call `exit`, but send_frame! should #86

Open focused opened 4 years ago

focused commented 4 years ago

I have a gen server that calls other module with Websockex.send_frame, sometimes send_frame exits and parent gen server process exits too, so the only way I can handle it is to use try/catch :exit.

The spec of send_frame tells us that it returns result tuple, but it can exit in abnormal situation. I think there should be two variants: send_frame - never exits and returns :ok/:error, send_frame! - returns or exits.

https://github.com/Azolo/websockex/blob/ce7064e1d6e8dfb1bba1bb8d3e75b71b88aabc1e/lib/websockex.ex#L413

@spec send_frame(client, frame) ::
          :ok
          | {:error,
             %WebSockex.FrameEncodeError{}
             | %WebSockex.ConnError{}
             | %WebSockex.NotConnectedError{}
             | %WebSockex.InvalidFrameError{}}
          | none
  def send_frame(client, _) when client == self() do
    raise %WebSockex.CallingSelfError{function: :send_frame}
  end

  def send_frame(client, frame) do
    try do
      {:ok, res} = :gen.call(client, :"$websockex_send", frame)
      res
    catch
      _, reason ->
        exit({reason, {__MODULE__, :call, [client, frame]}})
    end
  end