{"record":{"id":"36b41b7a649b8d20","repo":"apache/beam","slug":"failed-to-publish-v","errorCode":null,"errorMessage":"failed to publish '%v'","messagePattern":"failed to publish '(.+?)'","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/util/pubsubx/pubsub.go","lineNumber":115,"sourceCode":"}\n\nfunc publish(ctx context.Context, client *pubsub.Client, topic string, messages ...string) (*pubsub.Subscription, error) {\n\tt, err := EnsureTopic(ctx, client, topic)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tsub, err := EnsureSubscription(ctx, client, topic, fmt.Sprintf(\"%v.sub.%v\", topic, time.Now().Unix()))\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tfor _, msg := range messages {\n\t\tm := &pubsub.Message{\n\t\t\tData: ([]byte)(msg),\n\t\t}\n\t\tid, err := t.Publish(ctx, m).Get(ctx)\n\t\tif err != nil {\n\t\t\treturn nil, errors.Wrapf(err, \"failed to publish '%v'\", msg)\n\t\t}\n\t\tlog.Infof(ctx, \"Published %v with id: %v\", msg, id)\n\t}\n\treturn sub, nil\n}\n","sourceCodeStart":97,"sourceCodeEnd":121,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/util/pubsubx/pubsub.go#L97-L121","documentation":"pubsubx.Publish publishes each message to a Google Cloud Pub/Sub topic and blocks on the result. When a message's publish future returns an error, it is wrapped as \"failed to publish '%v'\" so the failing message payload is identified.","triggerScenarios":"Calling pubsubx.Publish (or TestPublish) when pubsub.Client Publish fails for any message — e.g. topic doesn't exist, quota exceeded, permission denied, or context canceled while waiting on Get(ctx).","commonSituations":"Wrong GCP project or topic name; missing pubsub.publisher IAM role on the credentials; publishing after ctx deadline exceeded in slow integration tests; Pub/Sub service outage.","solutions":["Read the wrapped underlying error to see the Pub/Sub API cause (permission, quota, not found).","Verify the topic exists and the service account has roles/pubsub.publisher.","Increase the context timeout or retry with backoff for transient unavailability.","Confirm the data payload encodes correctly; check for oversized messages (>10MB)."],"exampleFix":"// before\nsub, err := pubsubx.Publish(ctx, client, topic, sub, messages)\n// after (ensure valid ctx with timeout and permissions)\nctx, cancel := context.WithTimeout(ctx, 2*time.Minute)\ndefer cancel()\nsub, err := pubsubx.Publish(ctx, client, topic, sub, messages)","handlingStrategy":"try-catch","validationCode":"if topic == nil || topic.String() == \"\" { return errors.New(\"topic must be initialized before publishing\") }","typeGuard":null,"tryCatchPattern":"sub, err := pubsubx.Publish(ctx, client, topic, sub, messages)\nvar apiErr *googleapi.Error\nif err != nil {\n    if errors.As(err, &apiErr) {\n        // inspect apiErr.Code: 403 permissions, 429 quota, 404 topic\n    }\n    // retry transient failures with backoff\n}","preventionTips":["Grant roles/pubsub.publisher to the publishing service account.","Use a context with a sane timeout and retry policy.","Validate topic existence with client.Topic(...).Exists(ctx) before publishing."],"tags":["gcp","pubsub","publish"],"backgroundTag":"api-error-response","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}