good

Early Return for Elixir

MIT License

Downloads
231
Stars
3
Committers
1

Good

Module Good enable you Early Return like error handling, by falling through.

import Good

User.changeset(%User{}, params)
|> Repo.insert
|> then(fn user ->
  {:ok, "Hello, #{user.name}!"}
end)
|> otherwise(fn changeset ->
  {:error, changeset.errors |> Enum.into(%{})}
end)
|> case do
  # Everything comes here anyway
  {:ok, message} -> json conn, message
  {:error, reason} -> put_status(conn, 500) |> json reason
end

Installation

If available in Hex, the package can be installed as:

  1. Add good to your list of dependencies in mix.exs:
      def deps do
        [{:good, "~> 0.0.1"}]
      end
  1. Ensure good is started before your application:
      def application do
        [applications: [:good]]
      end