{"record":{"id":"13f5e3bbc8369004","repo":"TooTallNate/Java-WebSocket","slug":"rejected-because-of","errorCode":null,"errorMessage":"rejected because of ","messagePattern":"rejected because of ","errorType":"exception","errorClass":"InvalidHandshakeException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/java_websocket/WebSocketImpl.java","lineNumber":729,"sourceCode":"\n  public void startHandshake(ClientHandshakeBuilder handshakedata)\n      throws InvalidHandshakeException {\n    // Store the Handshake Request we are about to send\n    this.handshakerequest = draft.postProcessHandshakeRequestAsClient(handshakedata);\n\n    resourceDescriptor = handshakedata.getResourceDescriptor();\n    assert (resourceDescriptor != null);\n\n    // Notify Listener\n    try {\n      wsl.onWebsocketHandshakeSentAsClient(this, this.handshakerequest);\n    } catch (InvalidDataException e) {\n      // Stop if the client code throws an exception\n      throw new InvalidHandshakeException(\"Handshake data rejected by client.\");\n    } catch (RuntimeException e) {\n      log.error(\"Exception in startHandshake\", e);\n      wsl.onWebsocketError(this, e);\n      throw new InvalidHandshakeException(\"rejected because of \" + e);\n    }\n\n    // Send\n    write(draft.createHandshake(this.handshakerequest));\n  }\n\n  private void write(ByteBuffer buf) {\n    log.trace(\"write({}): {}\", buf.remaining(),\n        buf.remaining() > 1000 ? \"too big to display\" : new String(buf.array()));\n\n    outQueue.add(buf);\n    wsl.onWriteDemand(this);\n  }\n\n  /**\n   * Write a list of bytebuffer (frames in binary form) into the outgoing queue\n   *\n   * @param bufs the list of bytebuffer","sourceCodeStart":711,"sourceCodeEnd":747,"githubUrl":"https://github.com/TooTallNate/Java-WebSocket/blob/afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d/src/main/java/org/java_websocket/WebSocketImpl.java#L711-L747","documentation":"Also in startHandshake: when the application's onWebsocketHandshakeSentAsClient callback throws an uncaught RuntimeException, the library logs it, notifies onWebsocketError, and rethrows it as InvalidHandshakeException with the message 'rejected because of ' + the exception. The suffix contains the original RuntimeException's toString.","triggerScenarios":"Any RuntimeException escaping your onWebsocketHandshakeSentAsClient implementation (NPE while reading a config field, ClassCastException on a header value, etc.) during client connect.","commonSituations":"Null pointer in custom handshake code accessing uninitialized members; bugs in header manipulation; exceptions from third-party code invoked inside the callback.","solutions":["Read the chained exception ('rejected because of java.lang.NullPointer...') to find the real cause.","Wrap risky logic inside onWebsocketHandshakeSentAsClient in try/catch or fix the underlying bug.","Avoid performing side-effectful or fallible operations in the handshake callback.","Catch InvalidHandshakeException around connect() and surface the cause to logs."],"exampleFix":"// before\npublic void onWebsocketHandshakeSentAsClient(WebSocket conn, ClientHandshake req) {\n  String token = config.auth.token.toUpperCase(); // NPE if token null\n}\n// after\npublic void onWebsocketHandshakeSentAsClient(WebSocket conn, ClientHandshake req) {\n  String token = config.auth != null && config.auth.token != null ? config.auth.token.toUpperCase() : null;\n}","handlingStrategy":"try-catch","validationCode":"// guard callback inputs\nObjects.requireNonNull(config, \"config\");","typeGuard":null,"tryCatchPattern":"try { client.connectBlocking(); } catch (InvalidHandshakeException e) {\n  // message starts with 'rejected because of' — inspect the chained RuntimeException cause\n  log.error(\"client code crashed during handshake\", e.getCause());\n}","preventionTips":["Wrap risky logic in the handshake callback in try/catch","Avoid fallible side effects inside onWebsocketHandshakeSentAsClient","Read the chained cause to find the real bug"],"tags":["handshake","runtime-exception","callback"],"backgroundTag":"handshake-rejected","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"}