{"record":{"id":"2607d493e33860a3","repo":"TooTallNate/Java-WebSocket","slug":"unknown-role","errorCode":null,"errorMessage":"unknown role","messagePattern":"unknown role","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/java_websocket/drafts/Draft.java","lineNumber":272,"sourceCode":"\n  /**\n   * @deprecated use createHandshake without the role since it does not have any effect\n   */\n  @Deprecated\n  public List<ByteBuffer> createHandshake(Handshakedata handshakedata, Role ownrole,\n      boolean withcontent) {\n    return createHandshake(handshakedata, withcontent);\n  }\n\n  public List<ByteBuffer> createHandshake(Handshakedata handshakedata, boolean withcontent) {\n    StringBuilder bui = new StringBuilder(100);\n    if (handshakedata instanceof ClientHandshake) {\n      bui.append(\"GET \").append(((ClientHandshake) handshakedata).getResourceDescriptor())\n          .append(\" HTTP/1.1\");\n    } else if (handshakedata instanceof ServerHandshake) {\n      bui.append(\"HTTP/1.1 101 \").append(((ServerHandshake) handshakedata).getHttpStatusMessage());\n    } else {\n      throw new IllegalArgumentException(\"unknown role\");\n    }\n    bui.append(\"\\r\\n\");\n    Iterator<String> it = handshakedata.iterateHttpFields();\n    while (it.hasNext()) {\n      String fieldname = it.next();\n      String fieldvalue = handshakedata.getFieldValue(fieldname);\n      bui.append(fieldname);\n      bui.append(\": \");\n      bui.append(fieldvalue);\n      bui.append(\"\\r\\n\");\n    }\n    bui.append(\"\\r\\n\");\n    byte[] httpheader = Charsetfunctions.asciiBytes(bui.toString());\n\n    byte[] content = withcontent ? handshakedata.getContent() : null;\n    ByteBuffer bytebuffer = ByteBuffer\n        .allocate((content == null ? 0 : content.length) + httpheader.length);\n    bytebuffer.put(httpheader);","sourceCodeStart":254,"sourceCodeEnd":290,"githubUrl":"https://github.com/TooTallNate/Java-WebSocket/blob/afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d/src/main/java/org/java_websocket/drafts/Draft.java#L254-L290","documentation":"Draft.createHandshake serializes a Handshakedata into raw HTTP bytes and needs to know whether it represents a client (GET request) or server (101 response) handshake. If the object is neither a ClientHandshake nor a ServerHandshake, it throws IllegalArgumentException(\"unknown role\").","triggerScenarios":"Passing a custom or mock Handshake implementation that implements neither ClientHandshake nor ServerHandshake into createHandshake (e.g. in tests or custom draft code).","commonSituations":"Writing a custom Draft subclass or unit tests with a hand-rolled Handshakedata implementation, refactoring that lost the Client/Server marker interface.","solutions":["Implement your handshake object as ClientHandshake (client requests) or ServerHandshake (server responses)","Use the library's own HandshakeImpl1Client/HandshakeImpl1Server classes instead of custom implementations","Set the role on the draft (draft.setRole(...)) and let the library construct the handshake"],"exampleFix":"// before\nHandshakedata hs = new MyHandshake();\ndraft.createHandshake(hs);\n// after\nHandshakedata hs = new HandshakeImpl1Client();\ndraft.createHandshake(hs);","handlingStrategy":"type-guard","validationCode":"private static boolean hasKnownRole(Handshakedata hs) {\n  return hs instanceof ClientHandshake || hs instanceof ServerHandshake;\n}","typeGuard":"boolean hasKnownRole(Handshakedata hs) {\n  return hs instanceof ClientHandshake || hs instanceof ServerHandshake;\n}","tryCatchPattern":"try {\n  draft.createHandshake(handshakedata);\n} catch (IllegalArgumentException e) {\n  logger.error(\"Handshake has no client/server role: {}\", e.getMessage());\n}","preventionTips":["Use HandshakeImpl1Client/HandshakeImpl1Server, not custom implementations","In tests, build handshakes through the library types","Keep the role marker interfaces on any custom handshake subclass"],"tags":["websocket","handshake","illegal-argument"],"backgroundTag":"invalid-argument","analyzedSha":"afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d","analyzedAt":"2026-09-09T14:39:47.546Z","contentChangedAt":"2026-09-09T14:39:47.546Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}