{"id":"cfcc13b6c9f52b3d","repo":"gofiber/fiber","slug":"failed-to-save-response-w","errorCode":null,"errorMessage":"failed to save response: %w","messagePattern":"failed to save response: %w","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"middleware/idempotency/idempotency.go","lineNumber":180,"sourceCode":"\t\t\t\t// Filter\n\t\t\t\tres.Headers = make(map[string][]string)\n\t\t\t\tfor h, vals := range headers {\n\t\t\t\t\tif shouldKeepHeader(h) {\n\t\t\t\t\t\tres.Headers[h] = vals\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Marshal response\n\t\tbs, err := res.MarshalMsg(nil)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"failed to marshal response: %w\", err)\n\t\t}\n\n\t\t// Store response\n\t\tif err := cfg.Storage.SetWithContext(c, key, bs, cfg.Lifetime); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to save response: %w\", err)\n\t\t}\n\n\t\t_ = c.Locals(localsKeyWasPutToCache, true)\n\n\t\treturn nil\n\t}\n}\n","sourceCodeStart":162,"sourceCodeEnd":188,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/middleware/idempotency/idempotency.go#L162-L188","documentation":"Returned by the middleware (idempotency.go:179-181) when cfg.Storage.SetWithContext fails to persist the freshly produced (marshaled) response for the idempotency key. This is a backend write failure and breaks the idempotency guarantee for subsequent retries.","triggerScenarios":"Storage backend write error: Redis OOM, MySQL dead connection, MongoDB write concern failure, context cancellation before SetWithContext returns, storage quota exhausted.","commonSituations":"Storage eviction policy dropping writes; storage out of disk/memory; TLS to storage resetting; request cancelled by client mid-write so the key is never recorded and a retry will re-execute the handler.","solutions":["Verify Storage health and capacity (memory, disk, connections).","Increase the storage write timeout relative to handler+marshal time.","Make downstream handlers idempotent on their own so a missing cache entry on retry is still safe.","Monitor this error — every occurrence is a broken idempotency contract."],"exampleFix":"// before\napp.Use(idempotency.New(idempotency.Config{Storage: redis.New()}))\n\n// after — pre-flight check + sized connection pool\nstore := redis.New(redis.Config{PoolSize: 64})\nif err := store.SetWithContext(context.Background(), \"hc\", []byte(\"ok\"), time.Minute); err != nil {\n    log.Fatal(err)\n}\napp.Use(idempotency.New(idempotency.Config{Storage: store}))","handlingStrategy":"retry","validationCode":"// startup write check\nctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)\ndefer cancel()\nif err := store.SetWithContext(ctx, \"__hc__\", []byte(\"ok\"), time.Minute); err != nil {\n    log.Fatalf(\"storage write failed: %v\", err)\n}","typeGuard":null,"tryCatchPattern":"// make downstream handlers idempotent themselves so a missed cache write on retry is still safe","preventionTips":["Ensure Storage has capacity (memory, disk, connections) for write spikes.","Monitor this error — each occurrence is a broken idempotency contract.","Design downstream handlers to be safely retryable independent of the cache."],"tags":["idempotency","storage","write","redis"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}