Handling exceptions for logging

I’m setting up logging (to a file) on an app I’m building and want to log specific things. I have a process that is listening to a message queue, and when it gets a message it will get the payload, search for stuff in the database and at the end save in the database. I’m using Repo.one in different places, and would like to log if Repo.one raises an exception because there are more than 1 records. Is using try/rescue the solution to this? I read that using try/rescue is not the best practice and I was expecting Repo.one to return an {:error, _} tuple but that’s not the case, it raises an exception. So in terms of logging, is using try/rescue the only option?

1 Like