{"record":{"id":"26bc13a1fec9d164","repo":"gofr-dev/gofr","slug":"connection-error","errorCode":null,"errorMessage":"connection error","messagePattern":"connection error","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"pkg/gofr/datasource/pubsub/nats/errors.go","lineNumber":21,"sourceCode":"import \"errors\"\n\nvar (\n\t// Client Errors.\n\terrServerNotProvided       = errors.New(\"client server address not provided\")\n\terrSubjectsNotProvided     = errors.New(\"subjects not provided\")\n\terrConsumerNotProvided     = errors.New(\"consumer name not provided\")\n\terrConsumerCreationError   = errors.New(\"consumer creation error\")\n\terrFailedToDeleteStream    = errors.New(\"failed to delete stream\")\n\terrPublishError            = errors.New(\"publish error\")\n\terrJetStreamNotConfigured  = errors.New(\"jStream is not configured\")\n\terrJetStreamCreationFailed = errors.New(\"jStream creation failed\")\n\terrJetStream               = errors.New(\"jStream error\")\n\terrCreateStream            = errors.New(\"create stream error\")\n\terrDeleteStream            = errors.New(\"delete stream error\")\n\terrGetStream               = errors.New(\"get stream error\")\n\terrCreateOrUpdateStream    = errors.New(\"create or update stream error\")\n\terrHandlerError            = errors.New(\"handler error\")\n\terrConnectionError         = errors.New(\"connection error\")\n\terrSubscriptionError       = errors.New(\"subscription error\")\n)\n","sourceCodeStart":3,"sourceCodeEnd":24,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/datasource/pubsub/nats/errors.go#L3-L24","documentation":"errConnectionError (errors.go:21) is the sentinel returned when the NATS client cannot establish or re-establish its connection to the broker — used in establishConnection and the retryConnect path. It wraps the underlying dial failure so callers see a consistent connection error from gofr's NATS datasource. Exercised by TestClient_establishConnection and TestClient_retryConnect.","triggerScenarios":"ConnectionManager Connect/establishConnection failing to dial the configured NATS server(s): wrong URL/host:port, server down, TLS/auth rejection, or all reconnect attempts exhausted in retryConnect.","commonSituations":"NATS_URL env var wrong or empty in the deployment environment; NATS cluster unreachable due to network policy/DNS; credentials or TLS certs rotated; server down during startup so initial connect fails.","solutions":["Verify the NATS server URL(s) in config/env (host, port, scheme nats:// vs tls://) are correct and reachable (`nats conn -s $NATS_URL`).","Confirm the NATS server is running and listening (`nats server info`); restart it if down.","Check credentials/TLS: auth token, user/password, or client certs must match server configuration.","Increase reconnect options (RetryOnFailedConnect, MaxReconnects, backoff) or ensure retryConnect has time before failing fast."],"exampleFix":"// before: unreachable server URL from stale env\n// NATS_URL=nats://localhost:4222  (server runs elsewhere)\n// after\n// NATS_URL=nats://nats.default.svc.cluster.local:4222\nif err := client.Connect(); err != nil {\n    return fmt.Errorf(\"connect to NATS: %w\", err)\n}","handlingStrategy":"retry","validationCode":"u, err := url.Parse(cfg.NatsURL)\nif err != nil || u.Scheme == \"\" || u.Host == \"\" {\n    return fmt.Errorf(\"invalid NATS URL %q\", cfg.NatsURL)\n}\nconn, err := net.DialTimeout(\"tcp\", u.Host, 2*time.Second)\nif err != nil {\n    return fmt.Errorf(\"NATS host %s unreachable: %w\", u.Host, err)\n}\nconn.Close()","typeGuard":null,"tryCatchPattern":"if err := client.Connect(); err != nil {\n    if errors.Is(err, errConnectionError) {\n        // exponential backoff reconnect loop before giving up\n        return retryConnect(client, 5)\n    }\n    return err\n}","preventionTips":["Verify NATS_URL env/config in every deployment before rollout.","Enable RetryOnFailedConnect and MaxReconnects so transient outages self-heal.","Use health/readiness probes tied to client connection state.","Check TLS certs and credentials rotation schedules against server config."],"tags":["nats","connection","network","gofr"],"backgroundTag":"connection-refused","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}