{"record":{"id":"0ba92d5df5f50382","repo":"TooTallNate/Java-WebSocket","slug":"1007-0ba92d","errorCode":"1007","errorMessage":"Received text is no valid utf8 string!","messagePattern":"Received text is no valid utf8 string!","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/java_websocket/framing/CloseFrame.java","lineNumber":229,"sourceCode":"  /**\n   * Get the message that closeframe is containing\n   *\n   * @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;","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/TooTallNate/Java-WebSocket/blob/afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d/src/main/java/org/java_websocket/framing/CloseFrame.java#L211-L247","documentation":"CloseFrame.isValid validates outgoing close frames. A close frame with close code 1007 (NO_UTF8) must carry no reason string — by convention 1007 means 'I received invalid UTF-8' and any reason would itself need to be valid UTF-8/consistent. If code == NO_UTF8 and a reason text is present, InvalidDataException(1007) is thrown.","triggerScenarios":"Constructing a CloseFrame (or calling WebSocket.close(1007, reason) / closeConnection paths) with close code 1007 and a non-empty reason string, then isValid() runs before the frame is sent.","commonSituations":"Applications trying to explain an invalid-UTF-8 condition by attaching a reason to code 1007; code paths that reuse a generic close(reason) helper with a fixed code map; tests that build CloseFrame payloads by hand.","solutions":["Send close code 1007 with an empty reason string.","If you need to include an explanatory message, use a different close code (e.g. 1000 NORMAL or an app-range code 4000-4999) with the reason.","Validate close code/reason combinations before constructing the frame."],"exampleFix":"// before\nCloseFrame frame = new CloseFrame(CloseFrame.NO_UTF8, \"bad utf8 from client\");\n\n// after\nCloseFrame frame = new CloseFrame(CloseFrame.NO_UTF8, \"\"); // no reason with 1007","handlingStrategy":"validation","validationCode":"// guard before building a close frame\nif (code == 1007 && reason != null && !reason.isEmpty()) {\n    throw new IllegalArgumentException(\"close code 1007 must not carry a reason\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    webSocket.close(1007, reason);\n} catch (InvalidDataException | WebsocketNotConnectedException e) {\n    logger.warn(\"invalid close frame parameters: {}\", e.getMessage());\n}","preventionTips":["Send 1007 with an empty reason","Use app-range codes (4000-4999) when an explanation is needed","Centralize close-code selection in one helper"],"tags":["websocket","close-frame","utf8","close-code","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"}