hashicorp/nomad · warning

could not serialize our own error: %v

Error message

could not serialize our own error: %v

What it means

Fallback path in `writeJSON`: when `json.Marshal(obj)` fails, Nomad tries to marshal an errorWrapper containing the marshal error; if even that fails, this error is returned. This is nearly impossible in practice since errorWrapper is a simple string struct, and indicates an extreme internal problem (e.g. writer already broken and a pathological marshal failure).

Source

Thrown at command/operator_debug.go:1550

		wrapped := errorWrapper{Error: apiErr.Error()}
		return writeJSON(wrapped, writer)
	}

	err = writeNDJSON(obj, writer)
	if err != nil {
		return err
	}
	return nil
}

// writeNDJSON writes a single Nomad API objects (or response error) to the
// archive file as a JSON object.
func writeJSON(obj any, writer io.Writer) error {
	buf, err := json.Marshal(obj)
	if err != nil {
		buf, err = json.Marshal(errorWrapper{Error: err.Error()})
		if err != nil {
			return fmt.Errorf("could not serialize our own error: %v", err)
		}
	}
	n, err := writer.Write(buf)
	if err != nil {
		return fmt.Errorf("write error, wrote %d bytes of %d: %v", n, len(buf), err)
	}
	return nil
}

// writeNDJSON writes a slice of Nomad API objects to the archive file as
// newline-delimited JSON objects.
func writeNDJSON[T any](data []T, writer io.Writer) error {
	for _, obj := range data {
		err := writeJSON(obj, writer)
		if err != nil {
			return fmt.Errorf("failed to write to file: %w", err)
		}
		_, err = writer.Write([]byte{'\n'})

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Verify you are running stock Nomad; a patched build may inject unserializable types into capture objects.
  2. Report upstream with the Nomad version and the debug command invocation.
  3. As a workaround, capture the failing API data via direct HTTP calls instead of the debug bundle.
  4. Check the wrapped %v message to identify the failing marshal reason.
Defensive patterns

Strategy: try-catch

Try / catch

if err := writeJSON(obj, w); err != nil {
    if strings.Contains(err.Error(), "could not serialize our own error") {
        // log the wrapped message, file an upstream bug with Nomad version
    }
    return err
}

Prevention

When it happens

Trigger: json.Marshal of the payload object fails (e.g. unsupported type like a channel or func field, or a cyclic structure in an API response object), and the subsequent marshal of the errorWrapper also fails.

Common situations: A custom/patched Nomad build returning API objects with unserializable fields; writing objects that embed channels, funcs, or cycles; extremely unusual given all Nomad API types are serializable.

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 hashicorp/nomad@482b49bf1a (2026-09-04). Data as JSON: /api/errors/ae6045d90dd36fee. Report an issue: GitHub.