{"record":{"id":"aa96eaee06cb0b1d","repo":"hashicorp/nomad","slug":"failed-to-write-to-file-w","errorCode":null,"errorMessage":"failed to write to file: %w","messagePattern":"failed to write to file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command/operator_debug.go","lineNumber":1566,"sourceCode":"\t\tbuf, err = json.Marshal(errorWrapper{Error: err.Error()})\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"could not serialize our own error: %v\", err)\n\t\t}\n\t}\n\tn, err := writer.Write(buf)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"write error, wrote %d bytes of %d: %v\", n, len(buf), err)\n\t}\n\treturn nil\n}\n\n// writeNDJSON writes a slice of Nomad API objects to the archive file as\n// newline-delimited JSON objects.\nfunc writeNDJSON[T any](data []T, writer io.Writer) error {\n\tfor _, obj := range data {\n\t\terr := writeJSON(obj, writer)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"failed to write to file: %w\", err)\n\t\t}\n\t\t_, err = writer.Write([]byte{'\\n'})\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"failed to write to file: %w\", err)\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// writeError writes a JSON error object to capture errors in the debug bundle without\n// reporting\nfunc (c *OperatorDebugCommand) writeError(dir, file string, err error) error {\n\tbytes, err := json.Marshal(errorWrapper{Error: err.Error()})\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn c.writeBytes(dir, file, bytes)","sourceCodeStart":1548,"sourceCodeEnd":1584,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/operator_debug.go#L1548-L1584","documentation":"writeNDJSON serializes a slice of Nomad API objects as newline-delimited JSON into the debug bundle archive file. This error wraps any failure from writeJSON (encoding/marshaling an object) encountered during that loop. It indicates the bundle collection could not persist one of the API payloads it gathered.","triggerScenarios":"writeJSON returns an error for one of the objects in the data slice while collecting nomad operator debug output (e.g. json.Marshal failing via an unsupported type or an underlying writer failure surfaced by the encoder).","commonSituations":"Debugging a Nomad cluster with `nomad operator debug`; a custom writer/pipe backing the file handle is closed or broken, or a marshal failure on an unexpected API response shape.","solutions":["Inspect the wrapped %w error to see whether it is a marshal failure or an I/O failure on the writer","Verify the target file/directory for the debug bundle is writable and the disk is not full","Re-run `nomad operator debug`; transient API responses can cause one-off marshal/I/O failures","Update Nomad; if a specific API object type fails to marshal, it may be a fixed client bug"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Go: ensure the output location is writable before collecting\nif f, err := os.Create(path.Join(outDir, \".writecheck\")); err != nil {\n    return fmt.Errorf(\"output dir not writable: %w\", err)\n} else {\n    f.Close()\n    os.Remove(f.Name())\n}","typeGuard":"func isMarshalErr(err error) bool {\n    var je *json.MarshalTypeError\n    return errors.As(err, &je)\n}","tryCatchPattern":"if err := writeNDJSON(objs, w); err != nil {\n    var wrapped error\n    errors.As(err, &wrapped) // unwrap the %w cause\n    log.Printf(\"ndjson write failed: %v\", err)\n    // fall back to stderr or skip the section\n}","preventionTips":["Check disk space on the node before running nomad operator debug","Ensure the -output directory exists and is writable by the CLI user","Keep Nomad CLI and server versions in sync","Inspect errors.Unwrap results to distinguish marshal vs I/O causes"],"tags":["io","json","debug-bundle"],"backgroundTag":"file-write-failed","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}