I added an interruptive error message within a try… except and this did not return the error (also the fields did not get highlighted). I struggled a while to find the issue, but once I remove the try… except part of my code, it worked. I’m not sure if it’s a bug or my own mistake. Would love to hear solutions to this.
I’d have to see a code snippet to give a detailed answer, but in general: the only thing that is actually shown to the user is a viktor usererror. Using a try except block you could of course raise a more generic python error and show that to the user, doing something along the lines of:
try:
# Do something
except Exception as e:
raise vkt.UserError(f"There was an error: {str(e)}")
This re-raises the generic error and shows it to the user in a VIKTOR error. Of course you could also catch different errors and raise different usererrors accordingly:
try:
some_function()
with open("file.log") as file:
read_data = file.read()
except FileNotFoundError as fnf_error:
raise vkt.UserError(f"{fnf_error}")
except RuntimeError as error:
raise vkt.UserError("some_function() function wasn't executed.")
A good general introduction to handling exceptions can be found in this realpython article