elixir-maru / maru

Elixir RESTful Framework
https://maru.readme.io
BSD 3-Clause "New" or "Revised" License
1.32k stars 84 forks source link

several rescue_from? #40

Closed sdesbure closed 7 years ago

sdesbure commented 7 years ago

Hello, I've got a definition like this:

defmodule MyApp.API do
  use Maru.Router
  mount MyApp.API.Test
 get do
    conn |> text("It works!")
  end

  rescue_from :all, as: e do
    conn
    |> put_status(500)
    |> text("ERROR: #{inspect e}")
  end
end

and in MyApp.API.Test:

defmodule MyApp.API.Test do
  use Maru.Router
  plug Plug.Logger

  namespace :test do
    params do
      requires :Product_ID, type: String
    end 

    get do
      json(conn, %{"Result" => "yes"}
    end

    rescue_from [ Maru.Exceptions.Validation, Maru.Exceptions.InvalidFormatter ] do
    conn                                                                                                   
      |> put_status(500)
      |> json(%{"error" => %{"code" => 0, 
                      "context" => "context", 
                      "message" => "this is an error"})
    end
  end
end 

As you see, I have 2 rescue_from and the inner one doesn't work. Regarding the implementation, it's seems to be OK. But if I remove the outer (rescue_from :all), the inner still doesn't work :( And if I put the inner in MyApp.API then my tests are failing (the error is not catched). Any idea how to do that?

I'm mimicking an already deployed app server so I don't really have the choice for the error handling output...

falood commented 7 years ago

Hi @sdesbure , this is a known issue. I have had too many personal things to do for almost a month, and I'll start hacking this weekend and fix this soon!

falood commented 7 years ago

Hi @sdesbure , I just pushed a commit to master for several rescue_from support, you can check these unittest to understand the general. But, rescue_from is bind to router, not endpoint. So you should keep it out of namespace resources and so on. If you have any question, leave me a message please.

falood commented 7 years ago

Could I close this issue now?

sdesbure commented 7 years ago

Hello, I'm deeply sorry, I forgot to close. It works! Thank you