Hello guys, whatsup ?!
So I am trying to understand how phoenix framework works with error handling but I don’t figure out yet.
For me it’s a quite magic yet and I don’t how programming a custom error handlings as Elixir/Phoenix way.
Can someone helps me with that issue ?
Also I created a recovery password method, how can i wrote this more likely as an elixir/phoenix programmer ?


1 Like
Please don’t use images for code, instead use code fences blocks:
def hello() do
IO.puts "Hello Matheus Mello"
end
5 Likes
I would not use local variable if not needed, and usually deal with case, or with instead of if. If possible, I would also try to replace the first condition with function pattern match (as first condition). But not in the case You mention…
I also don’t like to use pipe, if I can simply replace it with a function call.
One way to do it.
case Repo.get_by(Reseller, email: email) do
nil -> ...
_reseller -> ...
end
# or
case Repo.get_by(Reseller, email: email) do
%Reseller{} = _reseller -> ...
nil -> ...
end
But as You don’t use the reseller, just check if it is present, I would use Repo.exists? instead.
And You could even use if in that case 
2 Likes
In your specific example, I might do something like this
defmodule MyAppWeb.PasswordRecoveryController do
use MyApp, :controller
def create(conn, %{"email" => email}) do
case Accounts.send_password_recovery_email(email) do
:ok ->
send_resp(conn, 200, "")
{:error, :email_not_found} ->
send_resp(conn, 404, "")
end
end
end
def MyApp.Accounts do
def send_password_recovery_email(email) do
if Repo.get_by(email: email) do
send_recovery_email(email)
:ok
else
{:error, :email_not_found}
end
end
end
There are definitely some security issues with this approach where a hacker could send requests at the server and see if an email exists in your system.
Since you’re next to Elixir, I’d highly recommend installing phx_gen_auth into your application and exploring the code it generates. The generated code was written by José Valim and is an excellent learning resource about how to write Elixir code. I hope this helps!
5 Likes
I really appreciate that, thanks for your support
All of you
@kokolegorille
@Exadra37
2 Likes