wavetermdev/waveterm · error

failed to marshal JSON: %v

Error message

failed to marshal JSON: %v

What it means

Error from `wsh blocks list --json` when json.MarshalIndent fails to serialize the collected []BlockDetails. This is nearly impossible for plain data (all fields are JSON-safe), so in practice it indicates corrupted/unencodable data produced upstream, e.g. invalid UTF-8 in block metadata or a custom marshaler error.

Source

Thrown at cmd/wsh/cmd/wshcmd-blocks.go:211

		return nil
	}

	// Stable ordering for both JSON and table output
	sort.SliceStable(allBlocks, func(i, j int) bool {
		if allBlocks[i].WorkspaceId != allBlocks[j].WorkspaceId {
			return allBlocks[i].WorkspaceId < allBlocks[j].WorkspaceId
		}
		if allBlocks[i].TabId != allBlocks[j].TabId {
			return allBlocks[i].TabId < allBlocks[j].TabId
		}
		return allBlocks[i].BlockId < allBlocks[j].BlockId
	})

	// Output results
	if blocksJSON {
		bytes, err := json.MarshalIndent(allBlocks, "", "  ")
		if err != nil {
			return fmt.Errorf("failed to marshal JSON: %v", err)
		}
		WriteStdout("%s\n", string(bytes))
		return nil
	}
	w := tabwriter.NewWriter(WrappedStdout, 0, 0, 2, ' ', 0)
	defer w.Flush()
	fmt.Fprintf(w, "BLOCK ID\tWORKSPACE\tTAB ID\tVIEW\tCONTENT\n")

	for _, b := range allBlocks {
		blockID := b.BlockId
		if len(blockID) > 36 {
			blockID = blockID[:34] + ".."
		}
		view := strings.ToLower(b.View)
		if view == "" {
			view = "<unknown>"
		}
		var content string

View on GitHub (pinned to a4447c1563)

Solutions

  1. Retry — if it persists, inspect block metadata for invalid characters
  2. Use table output (omit --json) to see which block's data is malformed
  3. Update Wave Terminal if a known BlockDetails marshalling bug exists
  4. Sanitize/normalize string fields from remote sources before output

Example fix

// before
bytes, err := json.MarshalIndent(allBlocks, "", "  ")
// after
bytes, err := json.MarshalIndent(allBlocks, "", "  ")
if err != nil {
    return fmt.Errorf("failed to marshal JSON (bad block metadata?): %w", err)
}
Defensive patterns

Strategy: try-catch

Validate before calling

// sanity-check block strings before marshalling
for _, b := range allBlocks {
    if !utf8.ValidString(b.Title) { return fmt.Errorf("invalid UTF-8 in block metadata for %s", b.BlockId) }
}

Type guard

func jsonSafe(s string) bool { return utf8.ValidString(s) }

Try / catch

bytes, err := json.MarshalIndent(allBlocks, "", "  ")
if err != nil {
    // fall back to table output instead of failing
    printTable(allBlocks)
    return nil
}

Prevention

When it happens

Trigger: json.MarshalIndent(allBlocks) returns an error — unsupported type in BlockDetails, a MarshalJSON implementation erroring, or invalid string data (invalid UTF-8) originating from block metadata.

Common situations: Remote block names/titles containing binary garbage that breaks JSON encoding; a custom marshaller bug in BlockDetails; memory/alloc failure on a very large block set.

Related errors


AI-assisted analysis of wavetermdev/waveterm@a4447c1563 (2026-09-01). Data as JSON: /api/errors/3a07819e2f968c2c. Report an issue: GitHub.