{"id":"f245c67362758a8b","repo":"gofiber/fiber","slug":"format-handler-is-nil-for-media-type-q-at-index","errorCode":null,"errorMessage":"format handler is nil for media type %q at index %d","messagePattern":"format handler is nil for media type %q at index (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"res.go","lineNumber":437,"sourceCode":"// https://godoc.org/github.com/valyala/fasthttp#Response\nfunc (r *DefaultRes) Response() *fasthttp.Response {\n\treturn &r.c.fasthttp.Response\n}\n\n// Format performs content-negotiation on the Accept HTTP header.\n// It uses Accepts to select a proper format and calls the matching\n// user-provided handler function.\n// If no accepted format is found, and a format with MediaType \"default\" is given,\n// that default handler is called. If no format is found and no default is given,\n// StatusNotAcceptable is sent.\nfunc (r *DefaultRes) Format(handlers ...ResFmt) error {\n\tif len(handlers) == 0 {\n\t\treturn ErrNoHandlers\n\t}\n\n\tfor i, h := range handlers {\n\t\tif h.Handler == nil {\n\t\t\treturn fmt.Errorf(\"format handler is nil for media type %q at index %d\", h.MediaType, i)\n\t\t}\n\t}\n\n\tr.Vary(HeaderAccept)\n\n\t// Absent means the combined Accept view (RFC 9110 Section 5.2) is empty:\n\t// no field line, or a single empty one. Checked on the raw lines to skip\n\t// the join allocation that multi-line headers would pay.\n\taccepts := r.c.fasthttp.Request.Header.PeekAll(HeaderAccept)\n\tif len(accepts) == 0 || (len(accepts) == 1 && len(accepts[0]) == 0) {\n\t\t// Without an Accept header the client accepts any media type\n\t\t// (RFC 9110 Section 12.5.1), so pick the first non-default handler and\n\t\t// use its media type. The literal \"default\" is not a media type and\n\t\t// must not be emitted as a Content-Type value.\n\t\tfor _, h := range handlers {\n\t\t\tif h.MediaType != \"default\" {\n\t\t\t\tr.c.fasthttp.Response.Header.SetContentType(h.MediaType)\n\t\t\t\treturn h.Handler(r.c)","sourceCodeStart":419,"sourceCodeEnd":455,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/res.go#L419-L455","documentation":"Format() validates every ResFmt passed to it and rejects any whose Handler is nil, because invoking a nil Handler would panic. The error names the offending media type and slice index so you can locate the bad entry. This is a programmer error, not a runtime/environment condition.","triggerScenarios":"Calling c.Format(ResFmt{MediaType: \"application/json\"}) with the Handler field missing, or building a handlers slice where one branch leaves Handler nil.","commonSituations":"Conditionally assembling a ResFmt slice and forgetting a branch; copy-paste omission; passing a nil handler variable.","solutions":["Provide a non-nil fiber.Handler for every ResFmt.","Build the slice dynamically and skip/filter entries whose Handler is nil before calling Format."],"exampleFix":"// before\nc.Format(\n    ResFmt{MediaType: \"application/json\", Handler: jsonHandler},\n    ResFmt{MediaType: \"text/html\"}, // Handler missing -> error\n)\n\n// after\nc.Format(\n    ResFmt{MediaType: \"application/json\", Handler: jsonHandler},\n    ResFmt{MediaType: \"text/html\", Handler: htmlHandler},\n)","handlingStrategy":"validation","validationCode":"func cleanFmts(fmts []ResFmt) []ResFmt {\n    out := make([]ResFmt, 0, len(fmts))\n    for _, f := range fmts {\n        if f.Handler != nil {\n            out = append(out, f)\n        }\n    }\n    return out\n}\n// then: c.Format(cleanFmts(fmts)...)","typeGuard":null,"tryCatchPattern":"if err := c.Format(fmts...); err != nil {\n    if strings.Contains(err.Error(), \"format handler is nil\") {\n        // programming error: fix the ResFmt slice\n        panic(err)\n    }\n    return err\n}","preventionTips":["Never construct ResFmt without a Handler.","Filter nil-handler entries when building slices dynamically.","Add a unit test asserting all format handlers are non-nil."],"tags":["content-negotiation","validation","fiber"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}