Write errors in app logs without stopping the http request flow

Hello,

What I’m trying to do:
I am working with http requests and I would like to handle the errors with 5xx status code.

My problem is that in order to see the error in the app logs I must not catch it. But if I do not handle the error then I can not send back a response to the client.

Here is a brief summary about my request logic:

  1. Request comes in
  2. Validate request
  3. Prepare response
  4. Send response

If an error occurs between step 1 and 4. Anvil automatically logs it into the app logs, which can be easily searched. I would like to be able to handle errors between step 1 and 4, so I can send the response back and also add it to the app logs as error.

Unfortunately the App logs docs could not help with this issue. So I am asking here.

Do you guys have any solutions to this, or could this be a feature request?

That’s not strictly true.

By default, if an uncaught exception happens, Anvil will display that exception in the app logs. But, you can also put info into the app logs yourself by using print. So if you catch the exception, and in the except clause you use print to output the details of the exception, whatever you print will be in the app logs.

1 Like

That is right. My apologies my post is misunderstandable.

I can write into the logs with print, but I was wondering if I would be able to write similar logs just as Uncaught exceptions show up.
If my prints would be marked red just as the uncaught exceptions, it would be a little bit easier to catch from a glance.

For example the last row would be red as well.

However there is also search in the logs and I can just simply use that, to filter out my caught errors.
Looks like this is the only available option.

Here is an example that shows how to manage an error and see the traceback: How to handle unexpected disconnect in Uplink program? - #4 by stefano.menci

In this case the traceback is sent via email, but you could simply print it, so it is logged.

2 Likes

Thank you @stefano.menci. This is exactly what I was just thinking. You read my mind.

I will simply send my caught errors via emails. This way I will be notified, if something happened.