{"record":{"id":"87095911ed1684d6","repo":"VictoriaMetrics/VictoriaMetrics","slug":"s","errorCode":null,"errorMessage":"%s","messagePattern":"%s","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/httpserver/httpserver.go","lineNumber":782,"sourceCode":"\tfor _, arg := range args {\n\t\tif err, ok := arg.(error); ok && errors.As(err, &esc) {\n\t\t\tstatusCode = esc.StatusCode\n\t\t\tbreak\n\t\t}\n\t}\n\n\tif rwa, ok := w.(*responseWriterWithAbort); ok && rwa.sentHeaders {\n\t\t// HTTP status code has been already sent to client, so it cannot be sent again.\n\t\t// Just write errStr to the response and abort the client connection, so the client could notice the error.\n\t\t//\n\t\t// HTML-escape the errStr in order to protect from possible XSS, since the errStr may contain user input.\n\t\terrStrEscaped := html.EscapeString(errStr)\n\n\t\tfmt.Fprintf(w, \"\\n%s\\n\", errStrEscaped)\n\t\trwa.abort()\n\t\treturn\n\t}\n\thttp.Error(w, errStr, statusCode)\n}\n\n// logHTTPError logs the errStr with the client remote address and the request URI obtained from r.\nfunc logHTTPError(r *http.Request, errStr string) {\n\tremoteAddr := GetQuotedRemoteAddr(r)\n\trequestURI := GetRequestURI(r)\n\terrStr = fmt.Sprintf(\"remoteAddr: %s; requestURI: %s; %s\", remoteAddr, requestURI, errStr)\n\tlogger.WarnfSkipframes(2, \"%s\", errStr)\n}\n\n// ErrorWithStatusCode is error with HTTP status code.\n//\n// The given StatusCode is sent to client when the error is passed to Errorf.\ntype ErrorWithStatusCode struct {\n\tErr        error\n\tStatusCode int\n}\n","sourceCodeStart":764,"sourceCodeEnd":800,"githubUrl":"https://github.com/VictoriaMetrics/VictoriaMetrics/blob/5079fb58f1e8e62113f90c945ad71586c797d770/lib/httpserver/httpserver.go#L764-L800","documentation":"httpserver.Errorf writes a formatted error to the client and logs it with remote address and request URI. If response headers were already sent (responseWriterWithAbort.sentHeaders), it cannot send a status code, so it writes the (unescaped to wire, HTML-escaped variant when streaming) message into the body and aborts the connection to break keep-alive — the surfaced message is just the formatted errStr. Otherwise it calls http.Error with a status extracted from any ErrorWithStatusCode argument (default 400).","triggerScenarios":"Handlers calling Errorf(w, r, format, args...) — e.g. unsupported paths, invalid query params, insert/query failures — including when the status code was already committed and the connection must be aborted mid-response.","commonSituations":"Clients requesting unknown paths or malformed queries; errors occurring mid-stream after headers were sent (client sees truncated body plus aborted keep-alive connection); an ErrorWithStatusCode in args propagating 503s from the insert limiter.","solutions":["Read the response body / server log line (remoteAddr, requestURI, message) to identify the actual handler error","Fix the client request that triggered the handler error (correct path, query params, payload)","If the connection is being aborted mid-response, check server logs for the underlying cause rather than the truncated client message"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// validate request before hitting the server\nif !strings.HasPrefix(path, \"/api/v1/\") && !knownPaths[path] {\n    return fmt.Errorf(\"path %q is not supported; check server logs via Errorf output\", path)\n}","typeGuard":"func isHTTPServerError(err error) (*httpserver.ErrorWithStatusCode, bool) {\n    var esc *httpserver.ErrorWithStatusCode\n    ok := errors.As(err, &esc)\n    return esc, ok\n}","tryCatchPattern":"resp, err := doRequest(req)\nif err != nil {\n    return err\n}\nif resp.StatusCode >= 400 {\n    body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))\n    return fmt.Errorf(\"server rejected request (status %d): %s\", resp.StatusCode, strings.TrimSpace(string(body)))\n}\n// mid-stream abort case: headers already sent, connection closed\nif resp.Header.Get(\"Connection\") == \"close\" && bodyTruncated {\n    return fmt.Errorf(\"response aborted mid-stream; check server log for Errorf message\")\n}","preventionTips":["Check server logs (remoteAddr + requestURI prefix) to find the failing handler","Handle ErrorWithStatusCode status codes (e.g. 503) with retry on the client","Do not rely on the body of aborted responses — the error text may be HTML-escaped or truncated","Validate paths and query parameters against the API surface before sending"],"tags":["http","error-handling","httpserver","response-abort"],"backgroundTag":"http-request-error-response","analyzedSha":"5079fb58f1e8e62113f90c945ad71586c797d770","analyzedAt":"2026-09-03T18:10:26.153Z","contentChangedAt":"2026-09-03T18:10:26.153Z","schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}