{"record":{"id":"cdfed5c6fd69dd6b","repo":"dotnet/aspnetcore","slug":"invalid-message-type","errorCode":null,"errorMessage":"Invalid message type.","messagePattern":"Invalid message type\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/SignalR/clients/ts/signalr-protocol-msgpack/src/MessagePackHubProtocol.ts","lineNumber":123,"sourceCode":"                return this._writeInvocation(message as InvocationMessage);\n            case MessageType.StreamInvocation:\n                return this._writeStreamInvocation(message as StreamInvocationMessage);\n            case MessageType.StreamItem:\n                return this._writeStreamItem(message as StreamItemMessage);\n            case MessageType.Completion:\n                return this._writeCompletion(message as CompletionMessage);\n            case MessageType.Ping:\n                return BinaryMessageFormat.write(SERIALIZED_PING_MESSAGE);\n            case MessageType.CancelInvocation:\n                return this._writeCancelInvocation(message as CancelInvocationMessage);\n            case MessageType.Close:\n                return this._writeClose();\n            case MessageType.Ack:\n                return this._writeAck(message as AckMessage);\n            case MessageType.Sequence:\n                return this._writeSequence(message as SequenceMessage);\n            default:\n                throw new Error(\"Invalid message type.\");\n        }\n    }\n\n    private _parseMessage(input: Uint8Array, logger: ILogger): HubMessage | null {\n        if (input.length === 0) {\n            throw new Error(\"Invalid payload.\");\n        }\n\n        const properties = this._decoder.decode(input) as any;\n        if (properties.length === 0 || !(properties instanceof Array)) {\n            throw new Error(\"Invalid payload.\");\n        }\n\n        const messageType = properties[0] as MessageType;\n\n        switch (messageType) {\n            case MessageType.Invocation:\n                return this._createInvocationMessage(this._readHeaders(properties), properties);","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/SignalR/clients/ts/signalr-protocol-msgpack/src/MessagePackHubProtocol.ts#L105-L141","documentation":"Thrown in the write path (writeMessage switch default) when asked to serialize a HubMessage whose type is not one of Invocation, StreamInvocation, Completion, Ping, CancelInvocation, Close, Ack, or Sequence. It is a programmer-error guard: you constructed or passed a HubMessage with an unknown/invalid MessageType enum value.","triggerScenarios":"Calling protocol.write(...) (directly or via HubConnection.send/invoke/stream) with a hand-built message whose type field is undefined, numeric garbage, or a future enum value the protocol version does not know how to serialize.","commonSituations":"Building custom HubMessage objects; mismatched @microsoft/signalr-protocol-msgpack version vs @microsoft/signalr (MessageType enum drift); a stale message object captured before an upgrade.","solutions":["Verify the message.type is a known MessageType enum value before calling write.","Align @microsoft/signalr and @microsoft/signalr-protocol-msgpack to the same version so the enum mapping is consistent.","If writing a new message kind, you are extending the protocol — branch on type explicitly rather than relying on the default arm."],"exampleFix":"// before\nprotocol.write({ type: 99, ... }); // unknown enum → throws\n\n// after — guard known types before write\nif (!Object.values(MessageType).includes(msg.type)) {\n  throw new TypeError(`Refusing to write unknown MessageType ${msg.type}`);\n}\nconst bytes = protocol.write(msg);","handlingStrategy":"validation","validationCode":"const SERIALIZABLE = new Set([MessageType.Invocation, MessageType.StreamInvocation, MessageType.Completion, MessageType.Ping, MessageType.CancelInvocation, MessageType.Close, MessageType.Ack, MessageType.Sequence]);\nfunction isSerializableMessageType(t: unknown): boolean {\n  return SERIALIZABLE.has(t as MessageType);\n}\nif (!isSerializableMessageType(msg.type)) throw new TypeError(`Unsupported MessageType ${msg.type}`);","typeGuard":"function isKnownMessageType(t: unknown): t is MessageType {\n  return typeof t === 'number' && t >= 1 && t <= 7 || t === 8 || t === 9;\n}","tryCatchPattern":"try { return protocol.write(msg); }\ncatch (e) {\n  if (/Invalid message type/.test((e as Error).message)) {\n    throw new TypeError(`Refusing to serialize message of type ${(msg as any).type}`);\n  }\n  throw e;\n}","preventionTips":["Always construct HubMessages via the documented factory methods.","Pin @microsoft/signalr and @microsoft/signalr-protocol-msgpack to the same version.","Do not extend MessageType without also extending the protocol's write/parse branches."],"tags":["signalr","messagepack","messagetype","serialization","programming-error"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}