Strange `CaseClauseError`

Well, let’s think about the control flow you might want. get_user_by_email! uses Repo.get_by!. What happens if it doesn’t return any records? It raises an exception. This means that you’d need to handle it with try / catch in your controller, but this probably isn’t what you want. So I’d get rid of the ! on get_by as well as on get_user_by_email to start with.

So now the question: what does get_by return? Well, it returns either the struct that that you asked for, or nil. So you need to handle the result out of get_user_by_email based on whether the result is nil or not. if is one option, pattern matching on %Library.User{} is another. I’ll leave the actual refactoring of the controller action as an exercise for the reader.