Error Handling
You can define error handlers for your application. Error handlers are functions that are called when an error occurs in your application. You can define error handlers for specific error types or for all errors.
examples/errors/error_handlers.py
import arc
@arc.command
def command():
arc.print("We're going to throw an error")
raise RuntimeError("Something has gone wrong!")
@command.handle(RuntimeError)
def handle_exception(ctx: arc.Context, exc):
arc.print("handled!")
command()
Handlers are called for relavent errors in reverse to the order they are defined. If you define a handler for a general error type, then a handler for a specific error type, the more specific handler will be called first.
Bubbling Errors¶
If a particular error can't be handled by the current error handler, it will be passed to the next error handler. This is called bubbling. If no error handler can handle the error, the error will be passed to the default error handler.
examples/errors/bubbling.py
import arc
@arc.command
def command():
arc.print("We're going to throw an error")
raise RuntimeError("Something has gone wrong!")
@command.handle(Exception)
def handle_exception(ctx: arc.Context, exc):
arc.print("General exception handler")
@command.handle(RuntimeError)
def handle_runtime_error(ctx: arc.Context, exc):
arc.print("Cannot handle this error, bubbling")
raise exc
command()
$ python errors/bubbling.py
We're going to throw an error
Cannot handle this error, bubbling
General exception handler
Default Error Handling Behavior¶
If no error handler is defined, arc will use the default error handling behavior:
- For internal errors (errors that derive from
arc.errors.ArcError
), a formatted message for the error will be printed to the console and then the application will exit with a non-zero exit code. - All other errors will be bubbled to the Python runtime & a traceback will be printed to the console. Since this isn't a particularly graceful exit, it's recommended that you define error handlers for your application.