Yes, there is an internal interface for this. We weren’t 100% sure about the API, so it’s not autocompleted or documented yet, but it’s a totally legitimate need, so here’s a beta release for you!
Make a Module (that’s code you can import from client and server), and define an exception class that inherits from anvil.server.WrappedError
. Then call anvil.server_register_exception_type()
and give it a fully qualified name (to distinguish it from any other class with the same name).
import anvil.server
class MyError(anvil.server.AnvilWrappedError):
pass
anvil.server._register_exception_type('my_module.MyException', MyError)
Now, if you raise this error on the server…
import my_module
@anvil.server.callable
def my_function():
raise MyError("Oops")
…you can catch it from the client:
try:
anvil.server.call('my_function')
catch my_module.MyError as e:
print("Yikes!")
(NB you’ll need the latest version of the Uplink to throw these exceptions: pip install --upgrade anvil-uplink
. This will work for you already if you’re using the Full Python runtime; it will arrive in the next few hours for Restricted users.)
(NB #2: We will be documenting this as a public interface, but for now I’m going to leave it here in beta and wait for feedback )