In Elixir, is there an idiomatic way to check that a match has succeeded or failed without using error mechanisms like try/rescue
?
While the answer by @PatNowak is absolutely correct, I personally prefer to have a private function handler(s) with different clauses to make the code more structured:
Insertion:
changeset
|> Repo.insert()
|> handle_insert()
Handlers:
defp handle_insert({:ok, user}), do: happy_path
defp handle_insert({:error, changeset}), do: sad_path
defp handle_insert(whatever), do: Logger.debug()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With