moonD4rk/HackBrowserData · error

encode dump: %w

Error message

encode dump: %w

What it means

WriteJSON failed to serialize the Dump struct to the supplied io.Writer via encoding/json. It fires at serialization time — an invalid writer (closed/nil), an io.Writer returning an error, or a Dump containing a value JSON cannot encode — and wraps the underlying json error for context. The Dump itself was already built; only the output step failed.

Source

Thrown at masterkey/dump.go:67

	}
}

func currentHost() Host {
	h := Host{OS: runtime.GOOS, Arch: runtime.GOARCH}
	if name, err := os.Hostname(); err == nil {
		h.Hostname = name
	}
	if u, err := user.Current(); err == nil {
		h.User = u.Username
	}
	return h
}

func (d Dump) WriteJSON(w io.Writer) error {
	enc := json.NewEncoder(w)
	enc.SetIndent("", "  ")
	if err := enc.Encode(d); err != nil {
		return fmt.Errorf("encode dump: %w", err)
	}
	return nil
}

// ReadJSON parses a Dump and rejects any version this build can't interpret — a silent misparse of an
// unrecognized schema is worse than a clear error.
func ReadJSON(r io.Reader) (Dump, error) {
	var d Dump
	dec := json.NewDecoder(r)
	if err := dec.Decode(&d); err != nil {
		return Dump{}, fmt.Errorf("decode dump: %w", err)
	}
	if d.Version != DumpVersion {
		return Dump{}, fmt.Errorf("unsupported dump version %q (this build expects %q)", d.Version, DumpVersion)
	}
	return d, nil
}

View on GitHub (pinned to 0503d04d7a)

Solutions

  1. Check the wrapped %w error to distinguish an I/O failure on the writer from an unencodable value.
  2. Ensure the destination writer is open and writable (file exists, disk has space, pipe peer alive).
  3. Confirm all fields of Dump (including any new vault kinds) are JSON-serializable types or have MarshalJSON implementations.
  4. Add a round-trip test (WriteJSON followed by ReadJSON) when adding new fields to Dump.
Defensive patterns

Strategy: try-catch

Validate before calling

buf := &bytes.Buffer{}
if err := dump.WriteJSON(buf); err != nil {
	return fmt.Errorf("dump is not encodable: %w", err)
}

Try / catch

if err := dump.WriteJSON(out); err != nil {
	if errors.Is(err, syscall.EPIPE) {
		log.Warnf("output pipe closed early: %v", err)
	} else {
		return fmt.Errorf("encode dump: %w", err)
	}
}

Prevention

When it happens

Trigger: Calling WriteJSON with a writer that fails mid-write (closed pipe, full disk, broken connection), or a Dump containing values json cannot encode (channels, funcs, cyclic data).

Common situations: Piping the dump into a process that exited early (broken pipe); writing to a full or read-only file/stdout; a Dump containing unmarshalable custom types added by a new VaultKind.

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 moonD4rk/HackBrowserData@0503d04d7a (2026-09-06). Data as JSON: /api/errors/018a1956a8b58e64. Report an issue: GitHub.