Skip to content

Commit

Permalink
Add if_exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooklyn Zelenka committed Aug 28, 2016
1 parent 23893a2 commit 20363c5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
39 changes: 39 additions & 0 deletions lib/exceptional/control.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,43 @@ defmodule Exceptional.Control do
end
end
end

@doc ~S"""
Alias for `Exceptional.Control.branch`
## Examples
iex> use Exceptional.Control
...> if_exception 1, do: fn ex -> ex end.(), else: fn v -> v + 1 end.()
2
iex> use Exceptional.Control
...> if_exception ArgumentError.exception("error message") do
...> fn %{message: msg} -> msg end.()
...> else
...> fn v -> v end.()
...> end
"error message"
iex> use Exceptional.Control
...> ArgumentError.exception("error message")
...> |> if_exception do
...> fn %{message: msg} -> msg end.()
...> else
...> fn v -> v end.()
...> end
"error message"
"""
defmacro if_exception(maybe_exception, do: exception_do, else: value_do) do
quote do
maybe_exc = unquote(maybe_exception)
if Exception.exception?(maybe_exc) do
maybe_exc |> unquote(exception_do)
else
maybe_exc |> unquote(value_do)
end
end
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Exceptional.Mixfile do
name: "Exceptional",
description: "Helpers for Elixir exceptions",

version: "1.0.7",
version: "1.1.0",
elixir: "~> 1.3",

source_url: "https://github.com/expede/exceptional",
Expand Down

0 comments on commit 20363c5

Please sign in to comment.