{"record":{"id":"19a02681aa934419","repo":"juanfont/headscale","slug":"unmarshalling-key-json-w-raw-s","errorCode":null,"errorMessage":"unmarshalling key JSON: %w (raw: %s)","messagePattern":"unmarshalling key JSON: %w \\(raw: (.+?)\\)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/dev/main.go","lineNumber":304,"sourceCode":"\n\terr := json.Unmarshal(data, &user)\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"unmarshalling user JSON: %w (raw: %s)\", err, data)\n\t}\n\n\treturn user.ID, nil\n}\n\n// extractAuthKey parses the JSON output of \"preauthkeys create\" and\n// returns the key string.\nfunc extractAuthKey(data []byte) (string, error) {\n\tvar key struct {\n\t\tKey string `json:\"key\"`\n\t}\n\n\terr := json.Unmarshal(data, &key)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"unmarshalling key JSON: %w (raw: %s)\", err, data)\n\t}\n\n\tif key.Key == \"\" {\n\t\treturn \"\", errEmptyAuthKey\n\t}\n\n\treturn key.Key, nil\n}\n","sourceCodeStart":286,"sourceCodeEnd":313,"githubUrl":"https://github.com/juanfont/headscale/blob/565fd254d06c4c7f9a8cad1714a43445c79ba420/cmd/dev/main.go#L286-L313","documentation":"Returned by extractAuthKey in cmd/dev/main.go when json.Unmarshal fails on the output of 'headscale preauthkeys create'. The function expects JSON with a \"key\" field; non-JSON output (error text, help, empty) causes the unmarshal error, and the offending bytes are echoed in '(raw: ...)'.","triggerScenarios":"The dev tool runs 'headscale preauthkeys create --user ...' and pipes stdout to extractAuthKey. Fails when the command prints an error (e.g., nonexistent user, server unreachable, expired flags) or when the binary version emits a different output format.","commonSituations":"User referenced by the preauthkey command was not created earlier in the dev flow (step ordering bug); mismatched binary versions between cmd/dev and the headscale CLI; server error text going to stdout instead of stderr.","solutions":["Inspect the '(raw: ...)' payload in the error to see the actual command output","Run 'headscale preauthkeys create' by hand with the same arguments to surface the real failure","Verify the user exists before creating the preauthkey ('headscale users list')","Rebuild binaries so cmd/dev and the CLI agree on the output format"],"exampleFix":"// before\nkey, err := extractAuthKey(out)\n\n// after\nif err != nil {\n\treturn \"\", fmt.Errorf(\"preauthkeys create output not parseable (raw: %s): %w\", out, err)\n}\nkey, err := extractAuthKey(out)","handlingStrategy":"validation","validationCode":"if !json.Valid(out) {\n\treturn \"\", fmt.Errorf(\"preauthkeys create emitted non-JSON output: %s\", out)\n}","typeGuard":null,"tryCatchPattern":"if _, err := extractAuthKey(out); err != nil {\n\treturn fmt.Errorf(\"cannot proceed without auth key: %w\", err)\n}","preventionTips":["Create the user before creating preauthkeys in scripted flows","Log raw CLI output at debug level in the dev tool for post-mortem","Keep binary versions in lockstep"],"tags":["json","cli","dev-tooling","go"],"backgroundTag":null,"analyzedSha":"565fd254d06c4c7f9a8cad1714a43445c79ba420","analyzedAt":"2026-08-15T13:12:30.133Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}