elixir-web / weber

[WiP] Web framework for Elixir inspired by Rails [#WeberMVC at freenode]
http://elixir-web.github.io/weber/
MIT License
371 stars 33 forks source link

Allow raising specific exceptions for rendering unauthorized/other responses #196

Closed rcdilorenzo closed 10 years ago

rcdilorenzo commented 10 years ago

I've create this pull-request to enable raising a WeberControllerException to make data processing and rendering responses such as unauthorized (401) or not found (404) to stop the continuation of the rest of the action from the controller. I'll be adding another commit for documentation as long as this concept is approved to be merged, but the code below is a simple example of how this can be used. I created this for a project that I'm building using weber where I have authentication, but I want to verify the session value and if that fails immediately render 401 without continuing the function.

defmodule TestTestTest.Exceptions do
  use Weber.Controller

  layout false

  render_when_raise :unknown, {:text, 500, "An unknown error occurred", []}
  render_when_raise :unauthorized, {:text, 401, "Unauthorized", []}

  def unauthorized_action([], _conn) do
    if true do
      raise_and_render :unauthorized
    end
    {:json, 200, [], []}
  end

  def error_500_action([], _conn) do
    raise_and_render :unknown
    {:json, 200, [], []}
  end

end
0xAX commented 10 years ago

Thank you @rcdilorenzo, merged

rcdilorenzo commented 10 years ago

BTW, what is the plan on merging elixir-0.13.2 into master now that 0.13.2 of elixir is now released?

0xAX commented 10 years ago

@rcdilorenzo i think that elixir-0.13.2 support already in weber master

rcdilorenzo commented 10 years ago

So, could you merge it into master and delete the branch since this PR actually went into the elixir-0.13.2 branch?

rcdilorenzo commented 10 years ago

See #197.