Phoenix error updating
Now that we have a working example, let’s clean up the code a bit.
Given this, I created a function that accepts a conn and the data Ecto returned, and returns a new conn.
My assumption here is that a database call that returns nil indicates a not found error.
So I use pattern matching to return either the :ok atom (200) or the :not_found atom (404).
If that use exists, we use the update changeset with Ecto update to perform the update. So we now have basic get, post and put endpoints that work.
We’ll now work to do some cleanup the code and return more useful error messages.
Here is a simple example of changeset for our user model.