{"record":{"id":"cdf6ae11539e0d80","repo":"crowdsecurity/crowdsec","slug":"error-truncating-content-for-s-w","errorCode":null,"errorMessage":"error truncating content for %s: %w","messagePattern":"error truncating content for (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/alertcontext/alertcontext.go","lineNumber":113,"sourceCode":"\t}\n\n\treturn &Context{}\n}\n\n// Truncate the context map to fit in the context value length\nfunc TruncateContextMap(contextMap map[string][]string, contextValueLen int) ([]*models.MetaItems0, []error) {\n\tmetas := make([]*models.MetaItems0, 0)\n\terrors := make([]error, 0)\n\tac := getAlertContext()\n\n\tfor key, values := range contextMap {\n\t\tif len(values) == 0 {\n\t\t\tcontinue\n\t\t}\n\n\t\tvalueStr, err := TruncateContext(values, ac.ContextValueLen)\n\t\tif err != nil {\n\t\t\terrors = append(errors, fmt.Errorf(\"error truncating content for %s: %w\", key, err))\n\t\t\tcontinue\n\t\t}\n\n\t\tmeta := models.MetaItems0{\n\t\t\tKey:   key,\n\t\t\tValue: valueStr,\n\t\t}\n\t\tmetas = append(metas, &meta)\n\t}\n\n\treturn metas, errors\n}\n\n// Truncate an individual []string to fit in the context value length\nfunc TruncateContext(values []string, contextValueLen int) (string, error) {\n\tvalueByte, err := json.Marshal(values)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"unable to dump metas: %w\", err)","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/alertcontext/alertcontext.go#L95-L131","documentation":"TruncateContextMap converts evaluated context values to strings and enforces the configured maximum length per key via TruncateContext, so alerts fit console/DB limits. If TruncateContext itself errors (it JSON-marshals the values), the failure is collected per key as 'error truncating content for %s: %w' and that key is skipped rather than aborting the whole map.","triggerScenarios":"AppsecEventToContext or EventToContext calls TruncateContextMap; for a given key the []string values fail json.Marshal inside TruncateContext. In practice this is nearly impossible with plain strings, so it surfaces only if values contain data that breaks marshaling (unsupported channel types would be required — practically a defensive path) or contextValueLen handling leads to repeated marshal failures.","commonSituations":"Rare defensive branch: with []string input, json.Marshal cannot fail, so hitting this usually indicates an internal bug or a modified TruncateContext usage with non-marshallable content; errors are accumulated and reported together after the loop.","solutions":["Inspect the wrapped %w error to identify why json.Marshal failed for that key's values","Sanitize the context values before truncation (ensure they are plain strings)","Log/report the issue upstream if it reproduces — with []string input this indicates an internal invariant violation","Continue is intentional: other keys are still truncated and sent, so check the resulting alert for missing metadata for this key"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"for k, vals := range contextMap {\n    for _, v := range vals {\n        if _, err := json.Marshal(v); err != nil {\n            log.Warnf(\"non-serializable context value for %s\", k)\n        }\n    }\n}","typeGuard":"func stringSliceOK(v []string) bool { return v != nil && len(v) > 0 }","tryCatchPattern":"ctxMap, errs := ac.TruncateContextMap(values)\nif len(errs) > 0 {\n    for _, e := range errs { log.Warnf(\"context key skipped: %v\", e) }\n    // ctxMap is still valid for remaining keys; proceed\n}","preventionTips":["Ensure context evaluation always yields []string of plain strings","Monitor for this defensive branch — with []string input it indicates an internal bug","Keep alert context small and UTF-8 clean","Upgrade crowdsec if the error reproduces, it is not expected"],"tags":["context","truncation","json"],"backgroundTag":"json-marshal-failed","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"}