Rescue After blocks - Try Catch blocks - Trap exit signal - Information Tuples with Conditionals - {:error, …} or {:ok, …} - If else / case cond do - Railway Oriented Programming - WITH Clause (Elixir 1.2) - Pattern Matching in Function Level
Fresh start - Generally should not save the state and reload - Consider it like a restart your computer - Sometimes save data - Sometimes not - Ask this question to yourself: - Is restarting the process solve my problem, or NOT? - Samples (it depends!): - DB connection error, crash, then restart on init - Retryable
coming!" ** (RuntimeError) Oh no error is coming! Simple Raise with Type iex> raise ArgumentError, message: "Invalid arg!" ** (ArgumentError) Invalid arg! Simple Raise with Type defmodule SampleError do defexception message: "this is special one" end
e in KeyError -> IO.puts "missing :file_path option" e in File.Error -> IO.puts "unable to read file" e in SampleError -> IO.inspect e end Rescue Errors Catch With Pattern Matching in Rescue Clause
IO.puts "missing :file_path option" after IO.puts "I will print at both case!" end Rescue Errors and After Need something to execute on both failure and success cases - Increment metrics ?
catch the ‘exit’ signals - You can pass the value and fetch that value on catch block try do for x <- 0..10 do if x == 2, do: throw(x) IO.puts(x) end catch x -> "Caught: #{x}" end 0 1 "Caught: 2"
fetch_user(email) case maybe_user do {:ok, user} -> case password_match(user, password) do {:ok, true} -> case is_confirmed?(user) do ... end {:error, msg} -> {:error, msg} end {:error, msg} -> {:error, msg} end
generally functions has two forms - without bang! - some_function(....) - {:ok, result} - {:error, “Some message or any type of data”} - with bang! - some_function!(...) - result - Raise an error - Elixir ‘bang’ package - @bang {[list_of_func_name_arg_count_tuples], {CallbackModule, :callback_fn}} - https://github.com/mustafaturan/bang