Skip to content

Seperates error handler into a plug #54

Merged
merged 2 commits into from Feb 26, 2014

3 participants

@HashNuke
  • Adds Phoenix.Plugs.ErrorHandler

  • Plugs Phoenix.Plugs.ErrorHandler at the highest level, so that other custom plugs can handle errors.

  • Removes error handling at Dispatcher client and Phoenix.Router.

  • The exception message would look ugly right now. I followed the conversation b/w @chrismccord and @ericmj on IRC that the dispatcher gen_server is going away. The error will look better, once it's gone.

According to this PR, to handle custom errors, plugs that look like the following can be added to app to handle custom exceptions/errors:

defmodule MogoChat.AwesomeErrorHandler do
  @behaviour Plug.Wrapper

  import Plug.Connection
  import Phoenix.Controller

  def init(opts), do: opts

  def wrap(conn, _opts, fun) do
    try do
      fun.(conn)
    catch
      kind, AwesomeException[message: reason] ->
        html conn, 401, "Unauthorized awesomeness! Please find a bug to fix"
    end
  end
end

Right now, because of the Dispatcher gen_server, I'm wading through some extra info and the custom error handler looks like this - https://github.com/HashNuke/mogo-chat/blob/dcdd942a4aa078222539ca69af8a5ee80aa9dcd9/lib/mogo_chat/auth_error_handler.ex

@jeregrine
phoenixframework member

Would it make sense to extract all of this way and have your error handler be a function with definition

defmodule MogoChat.AwesomeErrorHandler do
  use ErrorHandler
  def catch(kind, exception_record, conn)
    html conn, 401, "Unauthorized awesomeness! Please find a bug to fix"
  end
end

Since 90% of that is boilerplate

@HashNuke

@jeregrine that sounds WAY better. :+1:

In another PR maybe. (just to get one thing in first to make everything else possible).

@jeregrine
phoenixframework member

up to @chrismccord once its in I could throw together the DSL for it and do a PR as well.

@HashNuke

Sent another PR to remove dispatcher - #55.

Once that PR is in, the following is how the errors will look like:
screen shot 2014-02-26 at 3 51 23 am

@chrismccord chrismccord merged commit 98cb22f into phoenixframework:master Feb 26, 2014
@chrismccord
phoenixframework member

I don't see the need for a macro unless we intend to allow user's to have custom error hooks. Am I missing something obvious? Thanks @HashNuke!

@jeregrine
phoenixframework member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.