Prevent Ecto.NoResultsError in logs in live views

In my live views, I go get! which will raise Ecto.NoResultsError when there is no result.

Live view handles it properly, as it does redirect to my 404 page.

Now, if the live view is connected (for example if I hit back after a live redirect and the object does no longer exist), I get the Ecto.NoResultsError error in the logs before the live view redirects to the error page.

The app works fine, but those Ecto.NoResultsError are “normal”, is there a way to prevent them in the logs?

If the error is “normal” I would suggest to handle it explicitly, e.g. use get/3 instead of get!/3 and in case of a nil return value, redirect the client to the desired page.
I think that would make it clearer, that it is okay for the query not to return a value.

Well, the error is not in the normal flow of operation, it is “exceptional”, it requires the user to navigate to a page that do not exist with live view, so this require the page to disappear while the user is navigating. Using get! simplifies the code a lot and make it easier to read, when we load the resource, we don’t want to handle the case when it does not exist, because this should not happen, except under special conditions which are “exceptional”.

But the error gravity is low, and for this reason, I’d like to not have it logged in our server logs.

1 Like