{"record":{"id":"924245e9611175a8","repo":"denoland/deno","slug":"invalidaccesserror","errorCode":"InvalidAccessError","errorMessage":"The close code must be either 1000 or in the range of 3000 to 4999: received ${code}","messagePattern":"The close code must be either 1000 or in the range of 3000 to 4999: received (.+?)","errorType":"exception","errorClass":"DOMException","httpStatus":null,"severity":"error","filePath":"ext/websocket/01_websocket.js","lineNumber":701,"sourceCode":"    webidl.assertBranded(this, WebSocketPrototype);\n    const prefix = \"Failed to execute 'close' on 'WebSocket'\";\n\n    if (code !== undefined) {\n      code = webidl.converters[\"unsigned short\"](code, prefix, \"Argument 1\", {\n        clamp: true,\n      });\n    }\n\n    if (reason !== undefined) {\n      reason = webidl.converters.USVString(reason, prefix, \"Argument 2\");\n    }\n\n    if (this[_role] === CLIENT) {\n      if (\n        code !== undefined &&\n        !(code === 1000 || (3000 <= code && code < 5000))\n      ) {\n        throw new DOMException(\n          `The close code must be either 1000 or in the range of 3000 to 4999: received ${code}`,\n          \"InvalidAccessError\",\n        );\n      }\n    }\n\n    if (\n      reason !== undefined &&\n      TypedArrayPrototypeGetByteLength(core.encode(reason)) > 123\n    ) {\n      throw new DOMException(\n        \"The close reason may not be longer than 123 bytes\",\n        \"SyntaxError\",\n      );\n    }\n\n    if (this[_cancelHandle]) {\n      // Cancel ongoing handshake.","sourceCodeStart":683,"sourceCodeEnd":719,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/websocket/01_websocket.js#L683-L719","documentation":"ws.close(code, reason) validates the code only for client-role sockets (this[_role] === CLIENT): it must be exactly 1000 or in 3000..4999. Anything else - 1001, 1011, 2000, 5000, 0, negatives - throws InvalidAccessError. Codes 1001-1015 are reserved for the peer/OS and cannot be set from client JS.","triggerScenarios":"ws.close(1001, 'going away'), ws.close(1011), ws.close(2000), ws.close(5000), or passing 0 because no code was intended.","commonSituations":"Copying close codes from server logs into client code; using HTTP status codes (404, 500) as close codes; assuming all 1xxx codes are settable.","solutions":["Use 1000 for normal closure or 3000-4999 for application-specific codes","Omit the code entirely (ws.close()) for the default 1000 close","Let the server send 1001/1011 - the client side cannot"],"exampleFix":"// before\nws.close(1001, 'going away');\n\n// after\nws.close(1000, 'going away'); // or a custom code like 4000","handlingStrategy":"validation","validationCode":"const isValidCloseCode = (code?: number): boolean =>\n  code === undefined || code === 1000 || (code >= 3000 && code <= 4999);\nif (!isValidCloseCode(code)) throw new RangeError(`bad close code: ${code}`);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only 1000 and 3000-4999 are settable from client JS","Keep application close codes in a shared 4000-4999 registry","Never map HTTP status codes onto close codes"],"tags":["websocket","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-14T00:17:10.932Z"}