{"record":{"id":"ae6045d90dd36fee","repo":"hashicorp/nomad","slug":"could-not-serialize-our-own-error-v","errorCode":null,"errorMessage":"could not serialize our own error: %v","messagePattern":"could not serialize our own error: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"command/operator_debug.go","lineNumber":1550,"sourceCode":"\t\twrapped := errorWrapper{Error: apiErr.Error()}\n\t\treturn writeJSON(wrapped, writer)\n\t}\n\n\terr = writeNDJSON(obj, writer)\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn nil\n}\n\n// writeNDJSON writes a single Nomad API objects (or response error) to the\n// archive file as a JSON object.\nfunc writeJSON(obj any, writer io.Writer) error {\n\tbuf, err := json.Marshal(obj)\n\tif err != nil {\n\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'})","sourceCodeStart":1532,"sourceCodeEnd":1568,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/operator_debug.go#L1532-L1568","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify you are running stock Nomad; a patched build may inject unserializable types into capture objects.","Report upstream with the Nomad version and the debug command invocation.","As a workaround, capture the failing API data via direct HTTP calls instead of the debug bundle.","Check the wrapped %v message to identify the failing marshal reason."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := writeJSON(obj, w); err != nil {\n    if strings.Contains(err.Error(), \"could not serialize our own error\") {\n        // log the wrapped message, file an upstream bug with Nomad version\n    }\n    return err\n}","preventionTips":["Run stock Nomad releases; patched builds may add unserializable fields to capture objects.","Report this error upstream — it indicates an internal invariant violation.","Keep the payload objects limited to documented Nomad API types."],"tags":["json","serialization","debug"],"backgroundTag":"json-marshal-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"}