OpenNHP/opennhp · error

failed to marshal evidence

Error message

failed to marshal evidence: %v

What it means

GetEvidenceWithAgentUuid builds an evidence map (measure/serial_number keyed by the agent unique id) and serializes it to JSON before zlib compression. This error wraps any json.Marshal failure. Since the map values are plain strings, failures are extremely rare but would indicate a marshaling-engine problem.

Solutions

  1. Log the wrapped err (%v) to see the underlying marshal failure detail
  2. Verify the evidence map only contains JSON-serializable values (strings, numbers)
  3. Check for custom MarshalJSON methods on values added to the evidence map

Example fix

// before
evidenceBytes, err := json.Marshal(evidence)
if err != nil {
	return nil, fmt.Errorf("failed to marshal evidence: %v", err)
}
// after
evidenceBytes, err := json.Marshal(evidence)
if err != nil {
	return nil, fmt.Errorf("failed to marshal evidence: %w", err)
}
Defensive patterns

Strategy: try-catch

Try / catch

evidence, err := GetEvidenceWithAgentUuid()
if err != nil {
	var marshalErr error
	if errors.Is(err, json.UnsupportedTypeError) || errors.As(err, &marshalErr) {
		// handle non-serializable evidence content
	}
	return err
}

Prevention

When it happens

Trigger: json.Marshal(evidence) returns an error inside GetEvidenceWithAgentUuid (nhp/core/wasm/engine/host.go:83). Practically only occurs if the evidence map contains unsupported types or a custom marshaler errors.

Common situations: Custom json.Marshaler implementations on map values returning errors; corrupted agentUniqueId producing a value type whose MarshalJSON fails after code changes.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


AI-assisted analysis of OpenNHP/opennhp@6e04ca5ff0 (2026-09-07). Data as JSON: /api/errors/69e7ae4f075b5dce. Report an issue: GitHub.

Appendix: source

Thrown at nhp/core/wasm/engine/host.go:83

	return compressedBody, nil
}

func GetEvidenceWithAgentUuid() ([]byte, error) {
	agentUniqueId, err := CalculateAgentUniqueId()
	if err != nil {
		return nil, fmt.Errorf("failed to get agent unique id: %v", err)
	}

	evidence := map[string]any{
		"test_purpose":  "this evidence is for testing purposes only",
		"measure":       agentUniqueId,
		"serial_number": agentUniqueId,
	}

	evidenceBytes, err := json.Marshal(evidence)
	if err != nil {
		return nil, fmt.Errorf("failed to marshal evidence: %v", err)
	}

	var buf bytes.Buffer
	w := zlib.NewWriter(&buf)
	_, err = w.Write(evidenceBytes)
	w.Close()
	if err != nil {
		return nil, fmt.Errorf("failed to compress response body: %w", err)
	}

	return buf.Bytes(), nil
}

func GetEvidence() (string, error) {
	evidence, err := GetEvidenceWithCCUrl()
	if err != nil {
		evidence, err = GetEvidenceWithAgentUuid()
		if err != nil {

View on GitHub (pinned to 6e04ca5ff0)