{"record":{"id":"cb83157966047985","repo":"grafana/k6","slug":"websocket-requires-valid-url-but-got-q-which-res","errorCode":null,"errorMessage":"WebSocket requires valid url, but got %q which resulted in %w","messagePattern":"WebSocket requires valid url, but got %q which resulted in %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/websockets/websockets.go","lineNumber":181,"sourceCode":"\n\t// Maybe have this after the goroutine below ?!?\n\tdefineWebsocket(rt, w)\n\n\tgo w.establishConnection(params)\n\treturn w.obj\n}\n\n// parseURL parses the url from the first constructor calls argument or returns an error\nfunc parseURL(urlValue sobek.Value) (*url.URL, error) {\n\tif common.IsNullish(urlValue) {\n\t\treturn nil, errors.New(\"WebSocket requires a url\")\n\t}\n\n\t// TODO: throw the SyntaxError (https://websockets.spec.whatwg.org/#dom-websocket-websocket)\n\turlString := urlValue.String()\n\turl, err := url.Parse(urlString)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"WebSocket requires valid url, but got %q which resulted in %w\", urlString, err)\n\t}\n\tif url.Scheme != \"ws\" && url.Scheme != \"wss\" {\n\t\treturn nil, fmt.Errorf(\"WebSocket requires url with scheme ws or wss, but got %q\", url.Scheme)\n\t}\n\tif url.Fragment != \"\" {\n\t\treturn nil, fmt.Errorf(\"WebSocket requires no url fragment, but got %q\", url.Fragment)\n\t}\n\n\treturn url, nil\n}\n\nconst (\n\tarraybufferBinaryType = \"arraybuffer\"\n\tblobBinaryType        = \"blob\"\n)\n\n// defineWebsocket defines all properties and methods for the WebSocket\nfunc defineWebsocket(rt *sobek.Runtime, w *webSocket) {","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/grafana/k6/blob/165bb3c1a492b7a4823907fe1e93a30645794737/internal/js/modules/k6/websockets/websockets.go#L163-L199","documentation":"Validation failure in the WebSocket constructor's URL parsing: the first argument was a non-null string, but Go's net/url.Parse rejected it as malformed. This is the k6-side equivalent of the browser's SyntaxError for an unparseable WebSocket URL — control characters, spaces, invalid percent-encoding, or a missing scheme are typical culprits, and the wrapped %w carries url.Parse's exact reason.","triggerScenarios":"Thrown at internal/js/modules/k6/websockets/websockets.go:170 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Check the URL string for typos, stray spaces, quotes, or control characters","Percent-encode any special characters in the URL before passing it to new WebSocket","Ensure the URL includes a scheme, e.g., ws:// or wss://","Log the exact value passed to the constructor to spot script-side string interpolation bugs"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"165bb3c1a492b7a4823907fe1e93a30645794737","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}