{"record":{"id":"b434aaddb6824c27","repo":"gorilla/websocket","slug":"websocket-bad-write-message-type","errorCode":null,"errorMessage":"websocket: bad write message type","messagePattern":"websocket: bad write message type","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"conn.go","lineNumber":178,"sourceCode":"\n// IsUnexpectedCloseError returns boolean indicating whether the error is a\n// *CloseError with a code not in the list of expected codes.\nfunc IsUnexpectedCloseError(err error, expectedCodes ...int) bool {\n\tif e, ok := err.(*CloseError); ok {\n\t\tfor _, code := range expectedCodes {\n\t\t\tif e.Code == code {\n\t\t\t\treturn false\n\t\t\t}\n\t\t}\n\t\treturn true\n\t}\n\treturn false\n}\n\nvar (\n\terrWriteTimeout        = &netError{msg: \"websocket: write timeout\", timeout: true, temporary: true}\n\terrUnexpectedEOF       = &CloseError{Code: CloseAbnormalClosure, Text: io.ErrUnexpectedEOF.Error()}\n\terrBadWriteOpCode      = errors.New(\"websocket: bad write message type\")\n\terrWriteClosed         = errors.New(\"websocket: write closed\")\n\terrInvalidControlFrame = errors.New(\"websocket: invalid control frame\")\n)\n\n// maskRand is an io.Reader for generating mask bytes. The reader is initialized\n// to crypto/rand Reader. Tests swap the reader to a math/rand reader for\n// reproducible results.\nvar maskRand = rand.Reader\n\n// newMaskKey returns a new 32 bit value for masking client frames.\nfunc newMaskKey() [4]byte {\n\tvar k [4]byte\n\t_, _ = io.ReadFull(maskRand, k[:])\n\treturn k\n}\n\nfunc isControl(frameType int) bool {\n\treturn frameType == CloseMessage || frameType == PingMessage || frameType == PongMessage","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/gorilla/websocket/blob/e064f32e3674d9d79a8fd417b5bc06fa5c6cad8f/conn.go#L160-L196","documentation":"errBadWriteOpCode is returned when WriteMessage is called with an invalid message type — anything other than TextMessage (1), BinaryMessage (2), or an allowed control opcode the write path accepts. The library validates the messageType before writing.","triggerScenarios":"Calling conn.WriteMessage with a messageType constant that is not websocket.TextMessage or websocket.BinaryMessage (e.g. 0, 3-10, or an uninitialized variable), including accidentally passing the message payload's first byte as the type.","commonSituations":"Passing a dynamic/int messageType computed elsewhere, off-by-one or swapped arguments like WriteMessage(data, websocket.TextMessage), or reusing opcode constants from another WebSocket library.","solutions":["Pass only websocket.TextMessage or websocket.BinaryMessage as the first argument to WriteMessage","Check for swapped arguments if using a variable message type","Validate any externally-configured message type against the two allowed constants"],"exampleFix":"// before\nif err := conn.WriteMessage(msgType, payload); err != nil { ... } // msgType is 0\n// after\nif msgType != websocket.TextMessage && msgType != websocket.BinaryMessage {\n    msgType = websocket.TextMessage\n}\nif err := conn.WriteMessage(msgType, payload); err != nil { ... }","handlingStrategy":"validation","validationCode":"func validMessageType(t int) bool {\n    return t == websocket.TextMessage || t == websocket.BinaryMessage\n}","typeGuard":"func isTextOrBinary(t int) bool {\n    return t == websocket.TextMessage || t == websocket.BinaryMessage\n}","tryCatchPattern":"if !validMessageType(msgType) {\n    return fmt.Errorf(\"invalid message type %d\", msgType)\n}\nif err := conn.WriteMessage(msgType, payload); err != nil {\n    return err\n}","preventionTips":["Only use the websocket.TextMessage/BinaryMessage constants","Watch argument order in WriteMessage(type, data) calls","Don't map opcodes from other websocket libraries into gorilla constants"],"tags":["websocket","opcode","validation"],"backgroundTag":"invalid-message-type","analyzedSha":"e064f32e3674d9d79a8fd417b5bc06fa5c6cad8f","analyzedAt":"2026-08-31T12:40:58.222Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}