{"record":{"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":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"res.go","lineNumber":441,"sourceCode":"\n// formatDefaultMediaType is the sentinel MediaType marking a Format handler as\n// the fallback. It is not a media type and is never emitted as a Content-Type.\nconst formatDefaultMediaType = \"default\"\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 != formatDefaultMediaType {\n\t\t\t\tr.c.fasthttp.Response.Header.SetContentType(h.MediaType)\n\t\t\t\treturn h.Handler(r.c)","sourceCodeStart":423,"sourceCodeEnd":459,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/res.go#L423-L459","documentation":"Thrown by DefaultRes.Format before any content negotiation begins, when one of the supplied ResFmt values has a nil Handler. Format validates every handler up front so that negotiation never selects a handler it cannot call. The message names the offending MediaType and the index in the handlers slice, making it a programmer-error guard, not a runtime/network condition.","triggerScenarios":"Calling c.Format(...) with a ResFmt literal whose Handler field was omitted (left nil), e.g. ResFmt{MediaType: \"application/json\"} with no Handler function attached. Often a copy-paste or partial-construction mistake.","commonSituations":"Building the handlers slice conditionally and forgetting to set Handler on one branch; refactoring that splits MediaType and Handler assignment and drops one; a typo omitting the Handler: key in the struct literal.","solutions":["Ensure every ResFmt passed to Format has a non-nil Handler.","Construct ResFmt values through a helper that requires the handler, so it cannot be omitted.","Add a unit test that asserts all handlers in the slice are non-nil before calling Format."],"exampleFix":"// before\nc.Format(\n    fiber.ResFmt{MediaType: \"application/json\"}, // Handler missing\n    fiber.ResFmt{MediaType: \"text/html\", Handler: htmlHandler},\n)\n// -> format handler is nil for media type \"application/json\" at index 0\n\n// after\nc.Format(\n    fiber.ResFmt{MediaType: \"application/json\", Handler: jsonHandler},\n    fiber.ResFmt{MediaType: \"text/html\", Handler: htmlHandler},\n)","handlingStrategy":"validation","validationCode":"// Validate the handlers slice before calling Format.\nfunc validateFormats(handlers []fiber.ResFmt) error {\n    for i, h := range handlers {\n        if h.Handler == nil {\n            return fmt.Errorf(\"handler nil at index %d (%q)\", i, h.MediaType)\n        }\n    }\n    return nil\n}\nif err := validateFormats(handlers); err != nil { return err }\nc.Format(handlers...)","typeGuard":"func allFormatHandlersSet(handlers []fiber.ResFmt) bool {\n    for _, h := range handlers {\n        if h.Handler == nil { return false }\n    }\n    return true\n}","tryCatchPattern":"// Use a constructor that makes a nil Handler impossible to express.\nfunc fmtJSON(h fiber.Handler) fiber.ResFmt { return fiber.ResFmt{MediaType: \"application/json\", Handler: h} }\nc.Format(fmtJSON(jsonHandler), fmtHTML(htmlHandler))","preventionTips":["Always set both MediaType and Handler in the same ResFmt literal.","Use small constructor helpers (fmtJSON, fmtHTML) so Handler cannot be omitted.","Add a unit test asserting all handlers are non-nil before calling Format.","Treat this error as a bug to fix in code, not a runtime condition to catch."],"tags":["res","format","validation","programming-error","content-negotiation"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}