{"record":{"id":"1ce10042325b8dc2","repo":"gofr-dev/gofr","slug":"internal-server-error","errorCode":null,"errorMessage":"internal server error","messagePattern":"internal server error","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"pkg/gofr/http/responder.go","lineNumber":15,"sourceCode":"package http\n\nimport (\n\t\"bytes\"\n\t\"encoding/json\"\n\t\"errors\"\n\t\"net/http\"\n\t\"reflect\"\n\t\"sync\"\n\n\tresTypes \"gofr.dev/pkg/gofr/http/response\"\n)\n\nvar (\n\terrEmptyResponse = errors.New(\"internal server error\")\n)\n\n// maxRespPooledBuf caps the capacity of a response buffer returned to the pool\n// so an occasional very large response does not permanently inflate every\n// pooled buffer.\nconst maxRespPooledBuf = 64 << 10\n\n// initialRespBufCap is the starting capacity of a freshly minted pooled buffer,\n// sized to hold a typical small JSON response without a reslice.\nconst initialRespBufCap = 512\n\n// respBufPool holds reusable buffers for encoding JSON response bodies. Encoding\n// into a pooled buffer avoids the fresh []byte json.Marshal returns on every\n// response and collapses the body + trailing newline into a single Write. A\n// process-wide pool is the idiomatic shape for this and is safe for concurrent\n// use by construction.\n//\n//nolint:gochecknoglobals // process-wide pool of reusable response-encode buffers.","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/http/responder.go#L1-L33","documentation":"errEmptyResponse is the sentinel error in pkg/gofr/http/responder.go that gofr's HTTP responder returns when a handler yields a nil/empty response that cannot be serialized into a valid HTTP reply. Rather than sending an empty body with a 200, the framework surfaces this as an 'internal server error' to the client, since it indicates the handler failed to produce any response payload.","triggerScenarios":"A handler registered via gofr returns nil (or a zero-value interface) as its response object, so getPublicKeys/determineResponse in the responder pipeline find nothing to encode and errEmptyResponse is raised.","commonSituations":"Handler logic that forgets its return statement value or returns nil on an early-exit path; refactoring a handler to a typed response but leaving a nil-returning branch; middleware swallowing the response object.","solutions":["Inspect the handler returning the empty response and ensure every code path returns a non-nil response object or a real error","Log the handler name and inputs to find which route produced the empty payload","Add unit tests (like Test_getPublicKeys) covering all return paths of the handler","If nil is legitimate, return an explicit empty-but-non-nil response struct instead"],"exampleFix":"// before\nfunc handler(ctx *gofr.Context) (any, error) {\n    if ctx.Param(\"id\") == \"\" {\n        return nil, nil // yields internal server error\n    }\n    return data, nil\n}\n// after\nfunc handler(ctx *gofr.Context) (any, error) {\n    if ctx.Param(\"id\") == \"\" {\n        return emptyResponse{}, nil\n    }\n    return data, nil\n}","handlingStrategy":"validation","validationCode":"resp, err := handler(ctx)\nif err != nil {\n    return err\n}\nif resp == nil {\n    return errors.New(\"handler returned nil response\")\n}","typeGuard":"func isNilResponse(v any) bool {\n    if v == nil { return true }\n    rv := reflect.ValueOf(v)\n    switch rv.Kind() {\n    case reflect.Ptr, reflect.Map, reflect.Slice, reflect.Interface:\n        return rv.IsNil()\n    }\n    return false\n}","tryCatchPattern":"if resp, err := doRequest(client, url); err != nil {\n    if errors.Is(err, errEmptyResponse) {\n        log.Printf(\"handler produced no response: %v\", err)\n        http.Error(w, \"no content produced\", http.StatusUnprocessableEntity)\n        return\n    }\n    return err\n}","preventionTips":["Never return nil, nil from a gofr handler; return an explicit error or empty response struct","Cover every branch of handlers with tests asserting a non-nil return","Use vet/linters to catch functions with nil returns on some paths"],"tags":["http","handler","nil-response"],"backgroundTag":"empty-handler-response","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}