How would I handle the errors here?

so I want to read a file like File.stream!("somefile.txt"), how would I use {:ok, …} … in this case or overall handle errors like if file could not be found

A simple function to do that:

def safe_file_stream(path_to_file) do
  case File.open(path_to_file) do
    {:ok, io_device} -> {:ok, IO.stream(io_device, :line)}
    error -> error
  end
end
2 Likes