{"record":{"id":"508fb5c7b6c37fd1","repo":"TooTallNate/Java-WebSocket","slug":"closecode-must-not-be-sent-over-the-wire-code","errorCode":null,"errorMessage":"closecode must not be sent over the wire: {code}","messagePattern":"closecode must not be sent over the wire: (.+?)","errorType":"exception","errorClass":"InvalidFrameException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/java_websocket/framing/CloseFrame.java","lineNumber":241,"sourceCode":"  }\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) {\n      code = CloseFrame.NORMAL;\n    } else if (payload.remaining() == 1) {\n      code = CloseFrame.PROTOCOL_ERROR;\n    } else {\n      if (payload.remaining() >= 2) {\n        ByteBuffer bb = ByteBuffer.allocate(4);\n        bb.position(2);\n        bb.putShort(payload.getShort());\n        bb.position(0);","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/TooTallNate/Java-WebSocket/blob/afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d/src/main/java/org/java_websocket/framing/CloseFrame.java#L223-L259","documentation":"This is the final wire-safety check in CloseFrame.isValid: certain codes must never appear in a close frame sent over the network — ABNORMAL_CLOSE (1006), TLS_ERROR (1015), NOCODE (0), codes outside 1000-4999, and the reserved 1004. Violating this throws InvalidFrameException with the offending code embedded in the message.","triggerScenarios":"Constructing or sending a CloseFrame whose code is 1006, 1015, 0, 1004, < 1000, or > 4999; isValid() runs during frame construction/send (also invoked from WebSocket.close).","commonSituations":"Trying to echo back a 1006 abnormal closure received from the peer (1006 is never a wire code); forwarding I/O error codes like -1 or 0; using codes above 4999 for app semantics.","solutions":["Never send 1006/1015/0/1004 — these are generated internally by the library to signal local conditions.","Map desired meanings to allowed codes: 1000 normal, 1001 going away, 1011 server error, 4000-4999 app-defined.","Add a pre-send guard in your code that rejects codes in the forbidden set.","Inspect the message's trailing number to identify which illegal code was used."],"exampleFix":"// before\nwebSocket.close(1006, \"connection dropped\"); // 1006 never goes on the wire\n\n// after\nwebSocket.close(1001, \"connection dropped\");","handlingStrategy":"validation","validationCode":"static final Set<Integer> FORBIDDEN = Set.of(1004, 1005, 1006, 1015, 0);\nstatic boolean canSendCode(int code) {\n    return !FORBIDDEN.contains(code) && code >= 1000 && code <= 4999;\n}","typeGuard":null,"tryCatchPattern":"try {\n    webSocket.close(code, reason);\n} catch (InvalidFrameException e) {\n    logger.error(\"illegal close code attempted: {}\", e.getMessage());\n    webSocket.close(1001); // safe substitute\n}","preventionTips":["Never echo 1006/1015 back to peers — they are internal-only signals","Map internal error numbers to legal close codes explicitly","Unit-test your close-code mapping table"],"tags":["websocket","close-frame","close-code","rfc6455","frame-validation"],"backgroundTag":"invalid-enum-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"}