{"record":{"id":"7a160215baefd805","repo":"TooTallNate/Java-WebSocket","slug":"1002-7a1602","errorCode":"1002","errorMessage":"A close frame must have a closecode if it has a reason","messagePattern":"A close frame must have a closecode if it has a reason","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/java_websocket/framing/CloseFrame.java","lineNumber":232,"sourceCode":"   * @return the message in this frame\n   */\n  public String getMessage() {\n    return reason;\n  }\n\n  @Override\n  public String toString() {\n    return super.toString() + \"code: \" + code;\n  }\n\n  @Override\n  public void isValid() throws InvalidDataException {\n    super.isValid();\n    if (code == CloseFrame.NO_UTF8 && reason.isEmpty()) {\n      throw new InvalidDataException(CloseFrame.NO_UTF8, \"Received text is no valid utf8 string!\");\n    }\n    if (code == CloseFrame.NOCODE && 0 < reason.length()) {\n      throw new InvalidDataException(PROTOCOL_ERROR,\n          \"A close frame must have a closecode if it has a reason\");\n    }\n    //Intentional check for code != CloseFrame.TLS_ERROR just to make sure even if the code earlier changes\n    if ((code > CloseFrame.TLS_ERROR && code < 3000)) {\n      throw new InvalidDataException(PROTOCOL_ERROR, \"Trying to send an illegal close code!\");\n    }\n    if (code == CloseFrame.ABNORMAL_CLOSE || code == CloseFrame.TLS_ERROR\n        || code == CloseFrame.NOCODE || code > 4999 || code < 1000 || code == 1004) {\n      throw new InvalidFrameException(\"closecode must not be sent over the wire: \" + code);\n    }\n  }\n\n  @Override\n  public void setPayload(ByteBuffer payload) {\n    code = CloseFrame.NOCODE;\n    reason = \"\";\n    payload.mark();\n    if (payload.remaining() == 0) {","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/TooTallNate/Java-WebSocket/blob/afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d/src/main/java/org/java_websocket/framing/CloseFrame.java#L214-L250","documentation":"CloseFrame.isValid enforces that a close frame carrying a reason string must have an actual close code. Close code 0 (NOCODE) is reserved for internal representation and is not valid on the wire; sending a reason with NOCODE is contradictory, so InvalidDataException(1002 PROTOCOL_ERROR) is thrown.","triggerScenarios":"Building a CloseFrame whose code is CloseFrame.NOCODE (0) while the reason string is non-empty, e.g. calling close with code 0 plus a message, then isValid() rejects it before sending.","commonSituations":"Passing 0 as the close code by default/accident along with a reason; unmapped error paths that forward raw codes; tests constructing close frames manually.","solutions":["Provide a valid close code (1000, 1001, 1011, or 3000-4999) whenever a reason string is included.","If no code is known, send NOCODE with an empty reason, or let the library generate the frame.","Clamp/validate application close codes to the allowed ranges before calling close(code, reason)."],"exampleFix":"// before\nwebSocket.close(0, \"shutting down\"); // invalid: NOCODE with reason\n\n// after\nwebSocket.close(1000, \"shutting down\");","handlingStrategy":"validation","validationCode":"// guard before calling close\nif ((code == 0 || code == CloseFrame.NOCODE) && reason != null && !reason.isEmpty()) {\n    throw new IllegalArgumentException(\"a close code is required when a reason is provided\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    webSocket.close(code, reason);\n} catch (InvalidDataException e) {\n    logger.warn(\"rejected close frame: {}\", e.getMessage());\n    webSocket.close(1000); // fall back to a valid frame\n}","preventionTips":["Never pass 0/NOCODE together with a reason","Default to 1000 when no specific code applies","Validate codes at the call site of close()"],"tags":["websocket","close-frame","close-code","protocol-error","validation"],"backgroundTag":"invalid-argument-value","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"}