slimtoolkit/slim · error

error encoding data - %v

Error message

error encoding data - %v

What it means

encodeEvent serializes a MonitorDataEvent to JSON with SetEscapeHTML(false); if json.Encoder.Encode returns an error it is wrapped as "error encoding data - %v". Encoding a concrete struct like this rarely fails — it typically requires unsupported types in the payload (e.g. channels, funcs, or cyclic data injected via interface fields).

Source

Thrown at pkg/mondel/mondel.go:190

		case <-ticker.C:
			// Flush the buffer every second
			if buf.Len() > 0 {
				if _, err := ref.output.Write(buf.Bytes()); err != nil {
					logger.Errorf("Error writing batch: %v", err)
				}
				buf.Reset()
			}
		}
	}
}

func encodeEvent(event *report.MonitorDataEvent) (string, error) {
	var b bytes.Buffer
	enc := json.NewEncoder(&b)
	enc.SetEscapeHTML(false)
	if err := enc.Encode(event); err != nil {
		return "", fmt.Errorf("error encoding data - %v", err)
	}

	return b.String(), nil
}

View on GitHub (pinned to 81940d17fa)

Solutions

  1. Inspect the wrapped error message naming the unsupported type and remove/convert that field.
  2. Fix any custom MarshalJSON implementations on event payload types.
  3. Ensure event data contains only JSON-serializable values (strings, numbers, bools, slices, maps, structs).

Example fix

// before
type MonitorDataEvent struct { Callback func() `json:"-"` } // func type breaks JSON
// after
type MonitorDataEvent struct { CallbackName string }
Defensive patterns

Strategy: try-catch

Validate before calling

if _, err := json.Marshal(event); err != nil {
    return fmt.Errorf("event not serializable: %w", err)
}

Type guard

func isJSONSerializable(v any) bool {
    var b []byte
    return json.Unmarshal(mustJSON(v), &b) == nil // or pre-validate with json.Marshal in a helper
}

Try / catch

s, err := encodeEvent(ev)
if err != nil && strings.Contains(err.Error(), "error encoding data") {
    log.Warnf("dropping unserializable monitor event: %v", err)
    return nil // or fall back to a minimal safe event
}

Prevention

When it happens

Trigger: A MonitorDataEvent containing a field json.Marshal cannot handle (func, channel, complex numbers) or a custom MarshalJSON method returning an error.

Common situations: Adding a field of an unsupported type to the monitoring event struct, a nested custom type with a broken MarshalJSON, or cyclic pointers in event data.

Related errors


AI-assisted analysis of slimtoolkit/slim@81940d17fa (2026-08-31). Data as JSON: /api/errors/29c3ddb03b0a0cab. Report an issue: GitHub.