{"record":{"id":"4dfe35111ed51f01","repo":"crowdsecurity/crowdsec","slug":"while-building-request-w-4dfe35","errorCode":null,"errorMessage":"while building request: %w","messagePattern":"while building request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/apiclient/signal.go","lineNumber":19,"sourceCode":"package apiclient\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"net/http\"\n\n\t\"github.com/crowdsecurity/crowdsec/pkg/modelscapi\"\n\tlog \"github.com/sirupsen/logrus\"\n)\n\ntype SignalService service\n\nfunc (s *SignalService) Add(ctx context.Context, signals *modelscapi.AddSignalsRequest) (interface{}, *Response, error) {\n\tu := fmt.Sprintf(\"%s/signals\", s.client.URLPrefix)\n\n\treq, err := s.client.PrepareRequest(ctx, http.MethodPost, u, &signals)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"while building request: %w\", err)\n\t}\n\n\tvar response interface{}\n\n\tresp, err := s.client.Do(ctx, req, &response)\n\tif err != nil {\n\t\treturn nil, resp, fmt.Errorf(\"while performing request: %w\", err)\n\t}\n\n\tif resp.Response.StatusCode != http.StatusOK {\n\t\tlog.Warnf(\"Signal push response : http %s\", resp.Response.Status)\n\t} else {\n\t\tlog.Debugf(\"Signal push response : http %s\", resp.Response.Status)\n\t}\n\n\treturn &response, resp, nil\n}\n","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/apiclient/signal.go#L1-L37","documentation":"SignalService.Add builds the POST /signals request via PrepareRequest before sending alert signals to the API; if construction fails (trailing-slash BaseURL violation, URL parse error, JSON marshal failure of the AddSignalsRequest, gzip error), it wraps the error as 'while building request'. Nothing has been sent to the server yet — the failure is entirely client-side.","triggerScenarios":"s.client.PrepareRequest(ctx, http.MethodPost, u, &signals) returns an error before any network I/O: BaseURL without trailing slash, invalid URL prefix, or a signals body containing values the JSON encoder cannot marshal (e.g. NaN in a float field, unsupported types).","commonSituations":"Custom tooling building an ApiClient with a hand-parsed URL missing the trailing slash; ingesting alert data containing non-JSON-serializable values; version prefix/URLPrefix misconfigured so BaseURL.Parse fails.","solutions":["Inspect the wrapped inner error for the concrete cause","Verify BaseURL is absolute and its path ends with '/'","Ensure the AddSignalsRequest payload contains only JSON-serializable values (no NaN/Inf floats, channels, or funcs)","Validate the URLPrefix/version prefix configuration if the endpoint URL fails to parse"],"exampleFix":"// before\nreq, err := s.client.PrepareRequest(ctx, http.MethodPost, u, &signals)\nif err != nil {\n    return nil, nil, fmt.Errorf(\"while building request: %w\", err)\n}\n// after (pre-marshal validation)\nif err := validateSignalsJSONMarshalable(signals); err != nil {\n    return nil, nil, fmt.Errorf(\"signals payload not serializable: %w\", err)\n}\nreq, err := s.client.PrepareRequest(ctx, http.MethodPost, u, &signals)\nif err != nil {\n    return nil, nil, fmt.Errorf(\"while building request: %w\", err)\n}","handlingStrategy":"validation","validationCode":"// before calling SignalService.Add\nif !strings.HasSuffix(client.BaseURL.Path, \"/\") {\n    return errors.New(\"client BaseURL must end with a trailing slash\")\n}\nif _, err := json.Marshal(signals); err != nil {\n    return fmt.Errorf(\"signals payload not serializable: %w\", err)\n}","typeGuard":"func canSendSignals(client *apiclient.ApiClient) bool {\n    return client != nil && strings.HasSuffix(client.BaseURL.Path, \"/\")\n}","tryCatchPattern":"_, _, err := client.Signal.Add(ctx, signals)\nif err != nil {\n    if strings.Contains(err.Error(), \"while building request\") {\n        // construction-stage failure: validate URL and payload, no retry will help\n        return fmt.Errorf(\"invalid signal request: %w\", err)\n    }\n    return err\n}","preventionTips":["Sanitize alert data (no NaN/Inf floats) before building AddSignalsRequest","Normalize BaseURL with a trailing slash when constructing the client","Add a test that marshals a representative signals payload","Verify URLPrefix/version configuration matches the server's API version"],"tags":["request-building","signals","api-client"],"backgroundTag":"invalid-url-format","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}