{"record":{"id":"bc264ccb8f842f6c","repo":"PuerkitoBio/goquery","slug":"response-is-nil","errorCode":null,"errorMessage":"Response is nil","messagePattern":"Response is nil","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"type.go","lineNumber":66,"sourceCode":"// as html. It does not check if the reader is also an io.Closer, the\n// provided reader is never closed by this call. It is the responsibility\n// of the caller to close it if required.\nfunc NewDocumentFromReader(r io.Reader) (*Document, error) {\n\troot, e := html.Parse(r)\n\tif e != nil {\n\t\treturn nil, e\n\t}\n\treturn newDocument(root, nil), nil\n}\n\n// NewDocumentFromResponse is another Document constructor that takes an http response as argument.\n// It loads the specified response's document, parses it, and stores the root Document\n// node, ready to be manipulated. The response's body is closed on return.\n//\n// Deprecated: Use goquery.NewDocumentFromReader with the response's body.\nfunc NewDocumentFromResponse(res *http.Response) (*Document, error) {\n\tif res == nil {\n\t\treturn nil, errors.New(\"Response is nil\")\n\t}\n\tdefer res.Body.Close()\n\tif res.Request == nil {\n\t\treturn nil, errors.New(\"Response.Request is nil\")\n\t}\n\n\t// Parse the HTML into nodes\n\troot, e := html.Parse(res.Body)\n\tif e != nil {\n\t\treturn nil, e\n\t}\n\n\t// Create and fill the document\n\treturn newDocument(root, res.Request.URL), nil\n}\n\n// CloneDocument creates a deep-clone of a document.\nfunc CloneDocument(doc *Document) *Document {","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/PuerkitoBio/goquery/blob/738783cbc3c6244dea65b66306dae4fd0f84e7e3/type.go#L48-L84","documentation":"NewDocumentFromResponse returns this error when the *http.Response passed to it is nil. The function needs a live response whose body it will parse into a Document, so a nil response is rejected immediately before any parsing happens. It is a simple defensive guard added to prevent a nil-pointer panic inside the function.","triggerScenarios":"Calling goquery.NewDocumentFromResponse(nil) directly, or calling goquery.NewDocument (the deprecated convenience wrapper) with a nil *http.Response, typically when an HTTP client helper returned (nil, err) and the caller only checked one of the two return values.","commonSituations":"Ignoring the error from http.Client.Do / http.Get and passing the nil response onward; a helper function that returns *http.Response without propagating errors; old code written before NewDocumentFromReader existed that refactored to return nil on failure paths.","solutions":["Check that the *http.Response is non-nil before calling NewDocumentFromResponse, e.g. if res == nil { return nil }.","Inspect the error return of the HTTP call that produced the response; when err != nil the response is nil.","Migrate to goquery.NewDocumentFromReader(res.Body) after verifying res and res.Body yourself, as the deprecation notice recommends."],"exampleFix":"// before\nres, _ := http.Get(url)\ndoc, err := goquery.NewDocument(res) // panics-path: NewDocumentFromResponse(nil)\n\n// after\nres, err := http.Get(url)\nif err != nil {\n\treturn err\n}\ndoc, err := goquery.NewDocumentFromResponse(res)","handlingStrategy":"validation","validationCode":"if res == nil {\n\t// caller-side guard before goquery\n\treturn errors.New(\"no response to parse\")\n}\ndoc, err := goquery.NewDocumentFromResponse(res)","typeGuard":"func hasResponse(res *http.Response) bool { return res != nil }","tryCatchPattern":null,"preventionTips":["Never use _ to discard the error from http.Client.Do/Get; a nil response always accompanies a non-nil error.","Wrap HTTP fetching in a helper that returns (response, error) and always check both.","Prefer goquery.NewDocumentFromReader with an explicit body you control."],"tags":["go","goquery","nil-pointer","http-response"],"backgroundTag":"null-argument","analyzedSha":"738783cbc3c6244dea65b66306dae4fd0f84e7e3","analyzedAt":"2026-09-06T07:49:12.071Z","contentChangedAt":"2026-09-06T07:49:12.071Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}