{"record":{"id":"a1700f576ae85432","repo":"dotnet/aspnetcore","slug":"unexpected-message-type-d","errorCode":null,"errorMessage":"Unexpected message type: %d","messagePattern":"Unexpected message type: (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/SignalR/clients/java/signalr/messagepack/src/main/java/com/microsoft/signalr/messagepack/MessagePackHubProtocol.java","lineNumber":172,"sourceCode":"                    message = writeStreamItemMessage((StreamItem) hubMessage);\n                    break;\n                case COMPLETION:\n                    message = writeCompletionMessage((CompletionMessage) hubMessage);\n                    break;\n                case STREAM_INVOCATION:\n                    message = writeStreamInvocationMessage((StreamInvocationMessage) hubMessage);\n                    break;\n                case CANCEL_INVOCATION:\n                    message = writeCancelInvocationMessage((CancelInvocationMessage) hubMessage);\n                    break;\n                case PING:\n                    message = writePingMessage((PingMessage) hubMessage);\n                    break;\n                case CLOSE:\n                    message = writeCloseMessage((CloseMessage) hubMessage);\n                    break;\n                default:\n                    throw new RuntimeException(String.format(\"Unexpected message type: %d\", messageType.value));\n            }\n            int length = message.length;\n            List<Byte> header = Utils.getLengthHeader(length);\n            byte[] messageWithHeader = new byte[header.size() + length];\n            int headerSize = header.size();\n\n            // Write the length header, then all of the bytes of the original message\n            for (int i = 0; i < headerSize; i++) {\n                messageWithHeader[i] = header.get(i);\n            }\n            for (int i = 0; i < length; i++) {\n                messageWithHeader[i + headerSize] = message[i];\n            }\n\n            return ByteBuffer.wrap(messageWithHeader);\n        } catch (MessagePackException | IOException ex) {\n            throw new RuntimeException(\"Error writing MessagePack data.\", ex);\n        }","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/SignalR/clients/java/signalr/messagepack/src/main/java/com/microsoft/signalr/messagepack/MessagePackHubProtocol.java#L154-L190","documentation":"Thrown by MessagePackHubProtocol.writeMessage when serializing a HubMessage whose getMessageType() value does not match any case in the write switch (INVOCATION, STREAM_ITEM, COMPLETION, STREAM_INVOCATION, CANCEL_INVOCATION, PING, CLOSE). The Java MessagePack protocol only knows how to write the seven defined SignalR message kinds; anything else is a programming error or a protocol/client-version mismatch, not a recoverable wire condition.","triggerScenarios":"Calling protocol.writeMessage(...) with a custom HubMessage subclass whose getMessageType() returns a value outside the seven known HubMessageType enum members, or using a message type added in a newer server protocol that this older client build does not recognize. The switch falls through to the default branch at MessagePackHubProtocol.java:172.","commonSituations":"Client and server running incompatible SignalR protocol versions, a hand-built HubMessage passed into writeMessage, or an enum value corruption where messageType.value is not in the supported set.","solutions":["Only call writeMessage with one of the seven supported concrete message types; gate on hubMessage.getMessageType() before writing.","Ensure the SignalR Java client build matches the server's protocol version so no unknown message kinds are produced.","If you subclassed HubMessage, map it to a supported type instead of introducing a new getMessageType() value."],"exampleFix":"// before\nByteBuffer out = protocol.writeMessage(customMessage); // customMessage.getMessageType() is unsupported\n// after - only emit supported kinds\nif (EnumSet.of(INVOCATION, STREAM_ITEM, COMPLETION, STREAM_INVOCATION, CANCEL_INVOCATION, PING, CLOSE).contains(message.getMessageType())) {\n    ByteBuffer out = protocol.writeMessage(message);\n} else {\n    throw new IllegalArgumentException(\"Unsupported message type: \" + message.getMessageType());\n}","handlingStrategy":"type-guard","validationCode":"java.util.Set<HubMessageType> WRITABLE = java.util.EnumSet.of(\n    HubMessageType.INVOCATION, HubMessageType.STREAM_ITEM, HubMessageType.COMPLETION,\n    HubMessageType.STREAM_INVOCATION, HubMessageType.CANCEL_INVOCATION,\n    HubMessageType.PING, HubMessageType.CLOSE);\nif (!WRITABLE.contains(message.getMessageType())) {\n    throw new IllegalArgumentException(\"Cannot write message type \" + message.getMessageType());\n}","typeGuard":"// Only write messages the protocol can serialize\nstatic boolean isWriteable(HubMessage m) {\n    return m != null && java.util.EnumSet.of(\n        HubMessageType.INVOCATION, HubMessageType.STREAM_ITEM, HubMessageType.COMPLETION,\n        HubMessageType.STREAM_INVOCATION, HubMessageType.CANCEL_INVOCATION,\n        HubMessageType.PING, HubMessageType.CLOSE).contains(m.getMessageType());\n}","tryCatchPattern":"try {\n    ByteBuffer out = protocol.writeMessage(message);\n} catch (RuntimeException ex) {\n    if (ex.getMessage() != null && ex.getMessage().startsWith(\"Unexpected message type\")) {\n        // unsupported message kind - do not retry, fix the caller\n        throw new IllegalArgumentException(ex);\n    }\n    throw ex;\n}","preventionTips":["Never pass a custom HubMessage with an unrecognised getMessageType() into writeMessage.","Keep the client and server on the same SignalR protocol version.","Whitelist message types before invoking writeMessage."],"tags":["java","signalr","messagepack","serialization","protocol"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}