flipped-aurora/gin-vue-admin · error

序列化自动代码导出模板失败: %w

Error message

序列化自动代码导出模板失败: %w

What it means

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.

Source

Thrown at server/service/system/auto_code_persistence.go:132

		return fmt.Errorf("创建自动代码菜单 %s 失败: %w", desired.Name, err)
	}
	history.MenuID = desired.ID
	return nil
}

func persistAutoCodeExportTemplate(tx *gorm.DB, info request.AutoCode, history *request.SysAutoHistoryCreate) error {
	if !info.HasExcel {
		return nil
	}
	fields := make(map[string]string, len(info.Fields))
	for _, field := range info.Fields {
		if field != nil && field.Excel {
			fields[field.ColumnName] = field.FieldDesc
		}
	}
	templateInfo, err := json.Marshal(fields)
	if err != nil {
		return fmt.Errorf("序列化自动代码导出模板失败: %w", err)
	}
	name := info.Package + "_" + info.StructName
	entity := model.SysExportTemplate{
		DBName:       info.BusinessDB,
		Name:         name,
		TableName:    info.TableName,
		TemplateID:   name,
		TemplateInfo: string(templateInfo),
	}
	if err = tx.Create(&entity).Error; err != nil {
		return fmt.Errorf("创建自动代码导出模板 %s 失败: %w", name, err)
	}
	history.ExportTemplateID = entity.ID
	return nil
}

func autoCodeIdentityExists(ctx context.Context, db *gorm.DB, info request.AutoCode) (bool, error) {
	if db == nil {

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Confirm fields is still map[string]string in your copy; revert custom changes that put non-JSON-safe values in it.
  2. Log err via errors.Unwrap to see the exact unsupported type.
  3. If custom data must be stored, pre-sanitize it or marshal per-field with error tolerance.

Example fix

// before
fields[field.ColumnName] = field.FieldDesc
...
templateInfo, err := json.Marshal(fields)
if err != nil {
    return fmt.Errorf("序列化自动代码导出模板失败: %w", err)
}
// after
if fields == nil {
    fields = map[string]string{}
}
templateInfo, err := json.Marshal(fields)
if err != nil {
    logger.Errorf("export template marshal failed: %v", err)
    return fmt.Errorf("序列化自动代码导出模板失败: %w", err)
}
Defensive patterns

Strategy: validation

Validate before calling

// ensure the payload is serializable before persisting
if len(fields) == 0 {
    fields = map[string]string{}
}
if _, err := json.Marshal(fields); err != nil {
    return fmt.Errorf("fields not serializable: %w", err)
}

Type guard

func isJSONSafe(v any) bool {
    switch v.(type) {
    case chan struct{}, func(), unsafe.Pointer:
        return false
    }
    return true
}

Try / catch

templateInfo, err := json.Marshal(fields)
if err != nil {
    return fmt.Errorf("序列化自动代码导出模板失败: %w", err)
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/813877e0a0a049b1. Report an issue: GitHub.