{"record":{"id":"735947f9ef09ff06","repo":"larksuite/cli","slug":"protocol-decode-type-w","errorCode":null,"errorMessage":"protocol decode type: %w","messagePattern":"protocol decode type: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/event/adapter/localbus/protocol/codec.go","lineNumber":90,"sourceCode":"\t\t\tif len(buf)+len(chunk) > MaxFrameBytes {\n\t\t\t\treturn nil, ErrFrameTooLarge\n\t\t\t}\n\t\t\treturn append(buf, chunk...), nil\n\t\tcase bufio.ErrBufferFull:\n\t\t\tif len(buf)+len(chunk) > MaxFrameBytes {\n\t\t\t\treturn nil, ErrFrameTooLarge\n\t\t\t}\n\t\t\tbuf = append(buf, chunk...)\n\t\tdefault:\n\t\t\treturn nil, err\n\t\t}\n\t}\n}\n\nfunc Decode(line []byte) (interface{}, error) {\n\tvar env typeEnvelope\n\tif err := json.Unmarshal(line, &env); err != nil {\n\t\treturn nil, fmt.Errorf(\"protocol decode type: %w\", err)\n\t}\n\n\tvar msg interface{}\n\tswitch env.Type {\n\tcase MsgTypeHello:\n\t\tmsg = &Hello{}\n\tcase MsgTypeHelloAck:\n\t\tmsg = &HelloAck{}\n\tcase MsgTypeEvent:\n\t\tmsg = &Event{}\n\tcase MsgTypeBye:\n\t\tmsg = &Bye{}\n\tcase MsgTypePreShutdownCheck:\n\t\tmsg = &PreShutdownCheck{}\n\tcase MsgTypePreShutdownAck:\n\t\tmsg = &PreShutdownAck{}\n\tcase MsgTypeStatusQuery:\n\t\tmsg = &StatusQuery{}","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/larksuite/cli/blob/7fd6ef3c07182257ce776cdc5a614e122d5bd4b3/internal/event/adapter/localbus/protocol/codec.go#L72-L108","documentation":"protocol.Decode first unmarshals the line into a typeEnvelope just to read the 'type' discriminator. If the line is not valid JSON at all, it cannot even determine the type and returns this wrapped error. It is the first of two decode steps (the per-type unmarshal failure is wrapped separately as 'protocol decode %s').","triggerScenarios":"Decode receives a line that is not parseable JSON: empty line, partial write/truncated frame, plain-text garbage on the socket/pipe, or a frame split incorrectly by the newline framing (e.g. binary data injected into the stream).","commonSituations":"Writer process crashed mid-frame leaving a partial line; a log line got mixed into the bus pipe; client speaks a different/older protocol version writing non-JSON; reading a file that contains multiple concatenated non-newline-delimited blobs.","solutions":["Check the %w cause for the exact JSON syntax error offset and inspect the offending line content","Ensure the peer writes newline-delimited JSON only (use protocol.Encode) — no logging or binary writes to the shared pipe/socket","Verify both sides use the same protocol version/binary; restart the bus process to clear a corrupted stream","Make the reader tolerant: skip/discard empty or undecodable lines instead of treating the whole stream as fatal, if resync is acceptable"],"exampleFix":"// before: writing raw text into the pipe\nfmt.Fprintf(conn, \"hello %s\\n\", name)\n// after\nprotocol.Encode(conn, &protocol.Hello{Name: name})","handlingStrategy":"try-catch","validationCode":"func validFrame(line []byte) bool {\n\tline = bytes.TrimSpace(line)\n\tif len(line) == 0 || !json.Valid(line) {\n\t\treturn false\n\t}\n\treturn true\n}","typeGuard":"func isDecodeTypeError(err error) bool {\n\treturn err != nil && strings.HasPrefix(err.Error(), \"protocol decode type:\")\n}","tryCatchPattern":"msg, err := protocol.Decode(line)\nif err != nil {\n\tlog.Printf(\"dropping bad frame %q: %v\", line, err)\n\tcontinue // resync on next newline-delimited frame instead of aborting the stream\n}","preventionTips":["Only write to the bus stream via protocol.Encode; route logs elsewhere","Ensure frames are newline-terminated before reading the next line","Check for truncated writes: flush/close writers and handle partial-write errors","Verify both endpoints link the same protocol package version"],"tags":["go","json","localbus-protocol","framing"],"backgroundTag":"invalid-json","analyzedSha":"7fd6ef3c07182257ce776cdc5a614e122d5bd4b3","analyzedAt":"2026-09-04T21:17:44.649Z","contentChangedAt":"2026-09-04T21:17:44.649Z","schemaVersion":2},"datasetVersion":"2026-09-12T02:17:10.037Z"}