https://pulumi.com logo
#python
Title
# python
a

ambitious-father-68746

10/10/2023, 8:56 AM
Hi, in your opinion what would be the best way to end a Pulumi run earlier due to an foreseeable error? I've been raising exceptions, which triggers an exit, but they're not as eye-catching as doing a log.error().
d

dry-keyboard-94795

10/10/2023, 9:27 AM
You could do a
sys.exit(1)
after the log.error. To make it cleaner, wrap it in a function:
Copy code
def fatal(msg: str, resource: pulumi.Resource = None):
  log.error(msg, resource=resource)
  sys.exit(1)
You could also pass in the exception to the function and throw it after the log:
Copy code
msg = str(exc)
log.error(msg)
throw exc
Though I'm not sure how that will look visually
a

ambitious-father-68746

10/10/2023, 10:01 AM
The problem with sys.exit() is that it's not clean
And answering your last question, it would look something like:
Copy code
error: This is my custom error, it's all gone downhill
error: Program failed with an unhandled exception:
...
I think I'll stay with what I have
Thanks
d

dry-keyboard-94795

10/10/2023, 10:05 AM
I'm unsure what you mean by "not clean"
a

ambitious-father-68746

10/10/2023, 10:09 AM
It means I've confused it with
os._exit()
😅
d

dry-keyboard-94795

10/10/2023, 10:10 AM
Haha, OK
I read through the launcher script, it looks like you'll get a cleaner error by using
pulumi.RunError
as the base of your exceptions: https://www.pulumi.com/docs/reference/pkg/python/pulumi/#pulumi.RunError
a

ambitious-father-68746

10/10/2023, 10:12 AM
Oh, nice, thanks
With
pulumi.RunError
you essentially get what I want, which is a pretty error message followed by a clean exit, without a stack trace. 👍
5 Views