{"record":{"id":"e1735969057f488d","repo":"slimtoolkit/slim","slug":"error-encoding-updated-package-data","errorCode":null,"errorMessage":"error encoding updated package data","messagePattern":"error encoding updated package data","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/app/sensor/artifact/artifact.go","lineNumber":235,"sourceCode":"\t}\n\n\tversion, ok := info[\"version\"].(string)\n\tif !ok {\n\t\tlog.Tracef(\"nodePackageJSONVerUpdater - no version field, return as-is\")\n\t\treturn data, nil\n\t}\n\n\tversion = fmt.Sprintf(\"1%s\", version)\n\tlog.Tracef(\"nodePackageJSONVerUpdater(%s) - version='%v'->'%v')\\n\", target, info[\"version\"], version)\n\tinfo[\"version\"] = version\n\n\tvar b bytes.Buffer\n\tenc := json.NewEncoder(&b)\n\tenc.SetEscapeHTML(false)\n\tenc.SetIndent(\"  \", \"  \")\n\terr = enc.Encode(info)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error encoding updated package data\")\n\t}\n\n\treturn b.Bytes(), nil\n}\n\nvar fileTypeCmd string\n\nfunc init() {\n\tfindFileTypeCmd()\n}\n\nfunc findFileTypeCmd() {\n\tfileTypeCmd, err := exec.LookPath(fileTypeCmdName)\n\tif err != nil {\n\t\tlog.Debugf(\"findFileTypeCmd - cmd not found: %v\", err)\n\t\treturn\n\t}\n","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/slimtoolkit/slim/blob/81940d17fa112cc678e32209214bcb2355cb3004/pkg/app/sensor/artifact/artifact.go#L217-L253","documentation":"nodePackageJSONVerUpdater rewrites a package.json by re-encoding the parsed metadata with a json.Encoder into a buffer. If encoding the updated package info fails, it returns this error. Note the underlying error is discarded, so the message does not include the cause. In practice json.Encoder.Encode on an in-memory buffer rarely fails unless the value is invalid (e.g. contains an unsupported value such as a channel, func, or cyclic structure).","triggerScenarios":"Calling nodePackageJSONVerUpdater on package metadata that json cannot marshal — e.g. info was mutated to hold an unsupported type, or a custom marshaler on a field returns an error.","commonSituations":"Programmatic edits injecting non-JSON-safe values into the parsed package map; a custom MarshalJSON hook erroring; extremely rare buffer/encoder faults.","solutions":["Include the underlying error in the message (%w / %v) to diagnose the actual cause.","Validate that all fields added to info are JSON-serializable before encoding.","Marshal to a staging variable first and inspect it if the failure is reproducible.","Ensure any custom MarshalJSON implementations on the package-info types do not return errors."],"exampleFix":"// before\nif err != nil {\n    return nil, fmt.Errorf(\"error encoding updated package data\")\n}\n// after\nif err != nil {\n    return nil, fmt.Errorf(\"error encoding updated package data: %w\", err)\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"func isJSONSerializable(v any) bool {\n    _, err := json.Marshal(v)\n    return err == nil\n}","tryCatchPattern":"out, err := nodePackageJSONVerUpdater(pkgJSON, ver)\nif err != nil {\n    if strings.Contains(err.Error(), \"error encoding updated package data\") {\n        // re-parse and re-marshal to sanitize non-JSON-safe fields\n        var sanitized map[string]any\n        _ = json.Unmarshal(pkgJSON, &sanitized)\n        _ = json.Marshal(sanitized) // surfaces the real marshal error\n        return err\n    }\n    return err\n}","preventionTips":["Only put JSON-safe values into the parsed package info structure","Wrap the underlying encoder error with %w for diagnosability","Test round-trip encode of package.json structures in CI"],"tags":["json","encoding","sensor","artifact"],"backgroundTag":"json-encode-failed","analyzedSha":"81940d17fa112cc678e32209214bcb2355cb3004","analyzedAt":"2026-08-31T23:06:12.682Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}