Skip to content

Commit

Permalink
Merge pull request #197 from elixir-web/elixir-0.13.2
Browse files Browse the repository at this point in the history
Merge elixir-0.13.2 into master
  • Loading branch information
0xAX committed May 13, 2014
2 parents 3132e10 + 4cdce78 commit 29efe51
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 4 deletions.
38 changes: 37 additions & 1 deletion lib/weber/controller.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@

defexception WeberControllerException, [:message] do
def exception([value: value]) do
WeberControllerException[message: value]
end
end

defmodule Weber.Controller do

defmacro __using__(_) do
quote do
import Weber.Controller.Layout
import unquote(__MODULE__)
layout('Main.html')
@before_compile unquote(__MODULE__)
@render_on_raise []
end
end

end
defmacro __before_compile__(env) do
render_on_raise = Module.get_attribute(env.module, :render_on_raise)

quote do
def raise_keys do
unquote(render_on_raise) |> Enum.map(fn({k, v}) -> k end)
end

def render_value_for_key(key) do
{_, value} = unquote(render_on_raise) |> Enum.find(fn({k, v}) -> k == key end)
value
end
end
end

defmacro render_when_raise(value, result) do
result = Macro.escape(result)
quote do
@render_on_raise [{unquote(value), unquote(result)} | @render_on_raise]
end
end

def raise_and_render(value) do
raise WeberControllerException, value: value
end

end
17 changes: 14 additions & 3 deletions lib/weber/handler/weber_req_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Handler.WeberReqHandler do
import Weber.Route
import Weber.Session
import Weber.Http.Url
import Weber.Controller

require Lager

Expand Down Expand Up @@ -96,10 +97,20 @@ defmodule Handler.WeberReqHandler do
conn = case :lists.keyfind(:__before__, 2, controller.__info__(:functions)) do
false -> conn
_ -> controller.__before__(action, conn)
end
end

# get response from controller
result = Module.function(controller, action, 2).(getAllBinding(path, matched_path), conn)
result = try do
Module.function(controller, action, 2).(getAllBinding(path, matched_path), conn)
rescue
e ->
if e.message in controller.raise_keys do
controller.render_value_for_key(e.message)
else
IO.ANSI.escape("%{red} #{e.message}\n" <> Exception.format_stacktrace(System.stacktrace)) |> IO.puts
raise e, [], System.stacktrace
end
end
# handle controller's response, see in Handler.WeberReqHandler.Result
state.handler.handle_result(result, conn, controller, Weber.Utils.capitalize(atom_to_binary(action))) |> handle_request(req3, state, {controller, action, conn})
end
Expand Down
4 changes: 4 additions & 0 deletions lib/weber/handler/weber_req_handler_default.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ defmodule Handler.WeberReqHandler.Default do
{:text, 200, data, headers}
end

def request({:text, status, data, headers}, _app) do
{:text, status, data, headers}
end

def request({:json, data}, app) do
request({:json, data, []}, app)
end
Expand Down
22 changes: 22 additions & 0 deletions test/controllers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,25 @@ defmodule TestTestTest.JSON do
{:json, 201, [], []}
end
end

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
2 changes: 2 additions & 0 deletions test/route.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ defmodule Route do
|> on("GET", "/delete/:username/id/:id", :TestTestTest.Main, :delete_username_action)
|> on("GET", "/content_for", :TestTestTest.ContentFor, :content_for_action)
|> on("GET", "/partials", :TestTestTest.Partials, :partials)
|> on("GET", "/unauthorized", :TestTestTest.Exceptions, :unauthorized_action)
|> on("GET", "/unknown", :TestTestTest.Exceptions, :error_500_action)
|> redirect("GET", "/redirect", "/weber")

end
13 changes: 13 additions & 0 deletions test/weberTest/response_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,17 @@ defmodule WeberHttpResponseTest do
assert(body == {:ok, "<!DOCTYPE html>\n<html>\n <head>\n <title>\n My Project\n </title>\n <meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\" />\n </head>\n <body>\n <div id=\"container\">\n Hello Weber! \n </div>\n </body>\n</html> "})
end

test "raise unauthorized exception" do
{:ok, status, _, client} = :hackney.request(:get, 'http://localhost:8080/unauthorized', [], <<>>, [])
body = :hackney.body(client)
assert(status == 401)
assert(body == {:ok, "Unauthorized"})
end

test "raise 500 exception" do
{:ok, status, _, client} = :hackney.request(:get, 'http://localhost:8080/unknown', [], <<>>, [])
body = :hackney.body(client)
assert(status == 500)
assert(body == {:ok, "An unknown error occurred"})
end
end

0 comments on commit 29efe51

Please sign in to comment.