{"record":{"id":"813877e0a0a049b1","repo":"flipped-aurora/gin-vue-admin","slug":"w-813877","errorCode":null,"errorMessage":"序列化自动代码导出模板失败: %w","messagePattern":"序列化自动代码导出模板失败: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/auto_code_persistence.go","lineNumber":132,"sourceCode":"\t\treturn fmt.Errorf(\"创建自动代码菜单 %s 失败: %w\", desired.Name, err)\n\t}\n\thistory.MenuID = desired.ID\n\treturn nil\n}\n\nfunc persistAutoCodeExportTemplate(tx *gorm.DB, info request.AutoCode, history *request.SysAutoHistoryCreate) error {\n\tif !info.HasExcel {\n\t\treturn nil\n\t}\n\tfields := make(map[string]string, len(info.Fields))\n\tfor _, field := range info.Fields {\n\t\tif field != nil && field.Excel {\n\t\t\tfields[field.ColumnName] = field.FieldDesc\n\t\t}\n\t}\n\ttemplateInfo, err := json.Marshal(fields)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"序列化自动代码导出模板失败: %w\", err)\n\t}\n\tname := info.Package + \"_\" + info.StructName\n\tentity := model.SysExportTemplate{\n\t\tDBName:       info.BusinessDB,\n\t\tName:         name,\n\t\tTableName:    info.TableName,\n\t\tTemplateID:   name,\n\t\tTemplateInfo: string(templateInfo),\n\t}\n\tif err = tx.Create(&entity).Error; err != nil {\n\t\treturn fmt.Errorf(\"创建自动代码导出模板 %s 失败: %w\", name, err)\n\t}\n\thistory.ExportTemplateID = entity.ID\n\treturn nil\n}\n\nfunc autoCodeIdentityExists(ctx context.Context, db *gorm.DB, info request.AutoCode) (bool, error) {\n\tif db == nil {","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/auto_code_persistence.go#L114-L150","documentation":"Thrown by persistAutoCodeExportTemplate when json.Marshal(fields) fails while building the SysExportTemplate JSON payload from the auto-generated model's Excel-marked fields. This marshals a map[string]string, so failure is rare but possible with unsupported/recursive values introduced by customization.","triggerScenarios":"json.Marshal of the fields map returns an error during auto-code Create with export-template generation enabled — typically after someone changes the field collection to hold non-serializable values, or a custom marshaler on a field type returns an error.","commonSituations":"Customized generator templates injecting values into fields that json cannot encode; a forked codebase where fields was changed from map[string]string to something containing channels/funcs; Go runtime-level marshal errors.","solutions":["Confirm fields is still map[string]string in your copy; revert custom changes that put non-JSON-safe values in it.","Log err via errors.Unwrap to see the exact unsupported type.","If custom data must be stored, pre-sanitize it or marshal per-field with error tolerance."],"exampleFix":"// before\nfields[field.ColumnName] = field.FieldDesc\n...\ntemplateInfo, err := json.Marshal(fields)\nif err != nil {\n    return fmt.Errorf(\"序列化自动代码导出模板失败: %w\", err)\n}\n// after\nif fields == nil {\n    fields = map[string]string{}\n}\ntemplateInfo, err := json.Marshal(fields)\nif err != nil {\n    logger.Errorf(\"export template marshal failed: %v\", err)\n    return fmt.Errorf(\"序列化自动代码导出模板失败: %w\", err)\n}","handlingStrategy":"validation","validationCode":"// ensure the payload is serializable before persisting\nif len(fields) == 0 {\n    fields = map[string]string{}\n}\nif _, err := json.Marshal(fields); err != nil {\n    return fmt.Errorf(\"fields not serializable: %w\", err)\n}","typeGuard":"func isJSONSafe(v any) bool {\n    switch v.(type) {\n    case chan struct{}, func(), unsafe.Pointer:\n        return false\n    }\n    return true\n}","tryCatchPattern":"templateInfo, err := json.Marshal(fields)\nif err != nil {\n    return fmt.Errorf(\"序列化自动代码导出模板失败: %w\", err)\n}","preventionTips":["Keep fields as map[string]string.","Don't inject custom types into generator field maps.","Add a unit test marshaling the fields map."],"tags":["json","serialization","auto-code"],"backgroundTag":"json-marshal-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}