https://pulumi.com logo
#golang
Title
# golang
c

crooked-kitchen-90092

10/31/2023, 6:43 PM
what’s the intended pattern for the error-handling form of
ApplyT
? i’m naively trying to achieve something like the following:
Copy code
policyDocument, err := saml.Arn.ApplyT(func(arn string) (*iam.GetPolicyDocumentResult, error) {
  return iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
    Statements: []iam.GetPolicyDocumentStatement{
      {
        Principals: []iam.GetPolicyDocumentStatementPrincipal{
          {
            Type:        "Federated",
            Identifiers: []string{arn},
          },
        },
      },
    },
  })
})
if err != nil {
  return err
}
but of course
ApplyT
only returns a single value of type
internal.Output
. how would i bubble up an error from
iam.GetPolicyDocument
? or perhaps the question is, how should I use
ApplyT
correctly here?
a

ancient-policeman-24615

11/02/2023, 12:47 AM
If your plan is to fail the program if
err
is non-nil, then you are already using
ApplyT
correctly:
Copy code
policyDocument := saml.Arn.ApplyT(yourFunction)
If you want to handle the error and keep going, you will need to do so inside the `ApplyT`:
Copy code
policyDocument := saml.Arn.ApplyT(func(arn string) (*iam.GetPolicyDocumentResult, error) {
  doc, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
    Statements: []iam.GetPolicyDocumentStatement{
      {
        Principals: []iam.GetPolicyDocumentStatementPrincipal{
          {
            Type:        "Federated",
            Identifiers: []string{arn},
          },
        },
      },
    },
    if err != nil {
      return defaultDocument(arn)
     }
     return doc
  })
})
The first option is more common in Pulumi programs, but both are available.