elixir-maru / maru

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

DSL Discuss #105

Open falood opened 6 years ago

falood commented 6 years ago

Hidden variables or definite ones? Or better ideas?

  # Option 1
  get "path", fn conn, _params ->
    json(conn, %{hello: :hehe})
  end

  # Option 2
  get "path" do
    json(conn, %{hello: :hehe})
  end

  # Option 1
  rescue_from Unauthorized, fn conn, _params, error ->
    conn
    |> put_status(400)
    |> json(%{error: :unauthorized, error})
  end

  rescue_from :all, &rescue_all_errors/3

  # Option 2
  rescue_from Unauthorized, as: error do
    conn
    |> put_status(400)
    |> json(%{error: :unauthorized, error})
  end

  rescue_from :all, with: :rescue_all_errors
jalcine commented 6 years ago

Taking some time to look at this, I'm opting towards option 1.

I'm leaning towards it because it's a bit self-documenting and reads a bit more idiomatically for Elixir. In order to GET the route named "path", invoke this function.

The second one seems a bit more magical and doesn't give an explicit contract for the route handler.

falood commented 6 years ago

I also like option 1 better. but it will be quite different with plug router.