Can I return an error from anvil.server.connect()?

Hi there! Yes, you aren’t missing anything - that behaviour is what’s expected at the moment - it will continue to retry forever in the background.

There is a handy way to check your connection from a local script: use anvil.server.call(...). If that fails, it will throw an exception that tells you if there’s a connection problem.

import anvil.server

try:
    anvil.server.connect("Not an uplink key")
    anvil.server.call("some_function_in_your_server")
except Exception as e:
    print("Problem connecting: %s" % e)
    print("caught an exception, now we can handle the problem in here")

which prints

Problem connecting: Anvil fatal error: Incorrect uplink key
caught an exception, now we can handle the problem in here

I’ve added a feature request to improve anvil.server.connect() in the way you suggest. As a user on a Support contract you can ask us to prioritise certain features for you, so if this behaviour of anvil.server.connect is particularly getting in your way let me know and we can make improving it a priority.

1 Like