benbjohnson/litestream · error

failed to format response: %w

Error message

failed to format response: %w

What it means

Wraps a `json.MarshalIndent` failure in `litestream databases --json`. After collecting database status entries, the command tries to serialize them for output; if marshaling fails (e.g. an entry contains a value JSON cannot represent, like a NaN/Inf metric or a channel), the command returns this error instead of printing output. Rare in practice, since the data structures are plain.

Source

Thrown at cmd/litestream/databases.go:52

	}

	databases := make([]DatabaseInfo, 0, len(config.DBs))
	for _, dbConfig := range config.DBs {
		db, err := NewDBFromConfig(dbConfig)
		if err != nil {
			return err
		}

		databases = append(databases, DatabaseInfo{
			Path:    db.Path(),
			Replica: db.Replica.Client.Type(),
		})
	}

	if *jsonOutput {
		output, err := json.MarshalIndent(databases, "", "  ")
		if err != nil {
			return fmt.Errorf("failed to format response: %w", err)
		}
		fmt.Println(string(output))
		return nil
	}

	w := tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', 0)
	defer w.Flush()

	fmt.Fprintln(w, "path\treplica")
	for _, db := range databases {
		fmt.Fprintf(w, "%s\t%s\n", db.Path, db.Replica)
	}

	return nil
}

type DatabaseInfo struct {
	Path    string `json:"path"`

View on GitHub (pinned to 4ed7a308f6)

Solutions

  1. Report/inspect the wrapped error via `%w` to find the offending field
  2. Run the command without `--json` to get tabular output as a workaround
  3. Upgrade to the latest litestream version — this is likely a serialization bug
  4. If forking, validate fields added to the status struct are JSON-encodable

Example fix

// before
output, err := json.MarshalIndent(databases, "", "  ")
if err != nil {
	return fmt.Errorf("failed to format response: %w", err)
}
// after
output, err := json.MarshalIndent(databases, "", "  ")
if err != nil {
	log.Debug("json marshal failed; falling back to table output", "err", err)
	printTable(databases) // fallback path
	return nil
}
Defensive patterns

Strategy: fallback

Try / catch

output, err := json.MarshalIndent(databases, "", "  ")
if err != nil {
	// fall back to human-readable table output
	printTable(databases)
	return nil
}

Prevention

When it happens

Trigger: A database status field carrying a non-JSON-encodable value (unsupported type, invalid float); a bug in status collection placing unexpected data in the output struct; custom/derived builds where the databases slice holds exotic types.

Common situations: Running patched or forked litestream builds with extra status fields; hardware/OS quirks producing NaN timing values that json rejects.

Understand the failure class

Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.

Related errors


AI-assisted analysis of benbjohnson/litestream@4ed7a308f6 (2026-09-06). Data as JSON: /api/errors/a92554d506558fd6. Report an issue: GitHub.