{"record":{"id":"c425a385013bc9f8","repo":"denoland/deno","slug":"invalidaccesserror-c425a3","errorCode":"InvalidAccessError","errorMessage":"The close code must be either 1000 or in the range of 3000 to 4999.","messagePattern":"The close code must be either 1000 or in the range of 3000 to 4999\\.","errorType":"exception","errorClass":"DOMException","httpStatus":null,"severity":"error","filePath":"ext/websocket/02_websocketstream.js","lineNumber":460,"sourceCode":"        ],\n      }),\n      inspectOptions,\n    );\n  }\n}\nconst WebSocketStreamPrototype = WebSocketStream.prototype;\n\nfunction validateCloseCodeAndReason(closeInfo) {\n  if (!closeInfo.closeCode) {\n    closeInfo.closeCode = null;\n  }\n\n  if (\n    closeInfo.closeCode &&\n    !(closeInfo.closeCode === 1000 ||\n      (3000 <= closeInfo.closeCode && closeInfo.closeCode < 5000))\n  ) {\n    throw new DOMException(\n      \"The close code must be either 1000 or in the range of 3000 to 4999.\",\n      \"InvalidAccessError\",\n    );\n  }\n\n  const encoder = new TextEncoder();\n  if (\n    closeInfo.reason &&\n    TypedArrayPrototypeGetByteLength(encoder.encode(closeInfo.reason)) > 123\n  ) {\n    throw new DOMException(\n      \"The close reason may not be longer than 123 bytes.\",\n      \"SyntaxError\",\n    );\n  }\n}\n\nclass WebSocketError extends DOMException {","sourceCodeStart":442,"sourceCodeEnd":478,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/websocket/02_websocketstream.js#L442-L478","documentation":"validateCloseCodeAndReason checks closeInfo.closeCode for WebSocketStream.close({ closeCode, reason }): falsy values become null (no code sent); otherwise the code must be exactly 1000 or in 3000..4999, else an InvalidAccessError DOMException is thrown. A separate 123-byte reason check follows it.","triggerScenarios":"wss.close({ closeCode: 1001 }), { closeCode: 2000 }, { closeCode: 5000 }, or { closeCode: 0 } - reserved/system codes are not settable by the closing side.","commonSituations":"Reusing 1001/1011 codes seen in server logs for client-initiated closes; copying numeric codes from other protocols; passing 0 as a sentinel.","solutions":["Use 1000 or a 3000-4999 application code","Omit closeCode entirely for the default close","Define shared app close-code constants in the 4000-4999 range"],"exampleFix":"// before\nwss.close({ closeCode: 1001, reason: 'going away' });\n\n// after\nwss.close({ closeCode: 1000, reason: 'going away' });","handlingStrategy":"validation","validationCode":"const isValidCloseCode = (code?: number | null): boolean =>\n  !code || code === 1000 || (code >= 3000 && code <= 4999);\nif (!isValidCloseCode(closeInfo.closeCode)) {\n  closeInfo = { ...closeInfo, closeCode: 1000 };\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only 1000 and 3000-4999 are valid close codes","Keep close-code constants in one shared module","Omit closeCode when in doubt - null/undefined sends the default close"],"tags":["websocketstream","close-code","invalidaccesserror","validation"],"backgroundTag":"websocket-invalid-close-code","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}