hi team! tl;dr without `panic`, how can I handle e...
# golang
r
hi team! tl;dr without
panic
, how can I handle errors that my
ApplyT
function returns? I'd like my
pulumi preview
or
pulumi up
to exit upon error. I've read through docs and looked through source code and haven't found a solid answer. I'd like the following pseudocode to fail
pulumi preview
or
pulumi up
but it currently keeps running after I return an error. I would expect pulumi to detect that the
OutputState.err
field contains an error and abort the rest of the deployment. However, it just continues. Since this field is private, I have no way of extracting it either. The only option I've found is to panic but this doesn't actually reference the returned error (I'd need to explicitly panic within my function) so it seems like there was some intended way to use the returned error. Either the returned error is unused or I'm not looking in the right place. Any help would be appreciated! I expect this to bubble up the error but it doesn't:
Copy code
output := someStringOutput.ApplyT(func(someString string) (string, error) {
    return "", fmt.Errorf("always error")
})
If I panicked instead, then the returned error is useless (so no need to return an error, panic short-circuits the return anyway):
Copy code
output := someStringOutput.ApplyT(func(someString string) string {
    panic("always panic")
})
l
It's tricky to handle errors in a Apply. You can create a
err
reference before the apply and save the output in this var. So you can access it after the apply is done. But then you need to wait and it can be a blocker for your code.
r
Ahh, I've never considered that option. Thanks for the response, it's good to know that I'm not missing anything obvious
♥️ 1
l
I've played to much around with those applies. If you have some more complex step, you could also create a component. https://github.com/pulumi/pulumi-go-provider It is working very well for me. I recently used it to warp it over a OpenSource project. But the whole release process is a big overhead.
r
I'll certainly create my own components and consume them now that this PR has been merged. Thanks @echoing-dinner-19531!
b
@little-plumber-23857 @rapid-kangaroo-18476 can you put in code what you're describing here? https://pulumi-community.slack.com/archives/CCWP5TJ5U/p1753169704037199?thread_ts=1752776999.429269&cid=CCWP5TJ5U
Copy code
var err error
wait := make(chan struct{}, 1)
output := someStringOutput.ApplyT(func(someString string) (string) {
   defer close(wait)
    o, err = something()
    return o
}).(pulumi.StringOutput)
<-wait
if err != nil {
  return err
}
?