{"record":{"id":"99a6f967e3edd9a0","repo":"plandex-ai/plandex","slug":"error-marshalling-json-w","errorCode":null,"errorMessage":"error marshalling json: %w","messagePattern":"error marshalling json: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"app/cli/schema/schemas.go","lineNumber":57,"sourceCode":"\nfunc ValidateModelPackInlineJSON(jsonData []byte) (shared.ClientModelPackSchemaRoles, error) {\n\treturn validateJSON[shared.ClientModelPackSchemaRoles](jsonData, SchemaPathModelPackInline)\n}\n\nfunc validateJSON[T any](jsonData []byte, schemaPath SchemaPath) (T, error) {\n\tvar zero T\n\n\t// strip meta-keywords that break additionalProperties ──\n\tvar tmp interface{}\n\tif err := json.Unmarshal(jsonData, &tmp); err != nil {\n\t\treturn zero, fmt.Errorf(\"invalid json: %w\", err)\n\t}\n\tif obj, ok := tmp.(map[string]interface{}); ok {\n\t\tdelete(obj, \"$schema\") // ignore top-level $schema\n\t\tvar err error\n\t\tjsonData, err = json.Marshal(obj)\n\t\tif err != nil {\n\t\t\treturn zero, fmt.Errorf(\"error marshalling json: %w\", err)\n\t\t}\n\t}\n\n\tschemaLoader := newEmbeddedSchemaLoader(schemaPath)\n\tdocumentLoader := gojsonschema.NewBytesLoader(jsonData)\n\n\tresult, err := gojsonschema.Validate(schemaLoader, documentLoader)\n\tif err != nil {\n\t\treturn zero, err\n\t}\n\tif !result.Valid() {\n\t\tvar msgs []string\n\t\tfor _, d := range result.Errors() {\n\t\t\tmsgs = append(msgs, \"• \"+d.String())\n\t\t}\n\t\treturn zero, errors.New(strings.Join(msgs, \"\\n\"))\n\t}\n","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/cli/schema/schemas.go#L39-L75","documentation":"After stripping the top-level $schema key, validateJSON re-marshals the object back to bytes for the schema validator. This error means json.Marshal of the cleaned map failed — extremely rare for map[string]interface{} input derived from valid JSON, but possible with values the encoder cannot handle (e.g. channels, funcs injected programmatically).","triggerScenarios":"ValidateModelsInputJSON / ValidateModelPackInlineJSON receiving an object whose re-encoding fails after $schema removal — practically only when unmarshalable values were placed into the parsed structure.","commonSituations":"Programmatic callers constructing the input map with unsupported Go types; corruption of the decoded value by intermediary processing code.","solutions":["Ensure the input originates from valid JSON bytes, not raw Go structures","Check for non-serializable values injected before validation","Bypass $schema stripping (omit $schema) to skip the re-marshal path","Report as a bug if input is plain JSON and this still fires"],"exampleFix":"// before\ninput := map[string]interface{}{\"cb\": func() {}} // unmarshalable\n// after\ninput := []byte(`{\"name\":\"my-pack\"}`) // pass JSON bytes","handlingStrategy":"validation","validationCode":"var probe interface{}\nif err := json.Unmarshal(data, &probe); err != nil {\n    return err\n}\n// round-trip check catches unmarshalable values early\nif _, err := json.Marshal(probe); err != nil {\n    return fmt.Errorf(\"round-trip marshal failed: %w\", err)\n}","typeGuard":"func roundTrips(b []byte) bool {\n    var v interface{}\n    if json.Unmarshal(b, &v) != nil { return false }\n    _, err := json.Marshal(v)\n    return err == nil\n}","tryCatchPattern":"err := ValidateModelPackInlineJSON(data)\nif err != nil && strings.Contains(err.Error(), \"error marshalling json\") {\n    // input contained non-encodable values; log and report as a bug\n    return fmt.Errorf(\"validation pre-check failed: %w\", err)\n}","preventionTips":["Always pass real JSON bytes, not raw Go maps with arbitrary types","Keep $schema handling to the library (avoid manual mutation)","Round-trip test unusual inputs before validating"],"tags":["json","serialization","validation"],"backgroundTag":"json-marshal-failed","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}