{"record":{"id":"198912b52c5c6aca","repo":"karatelabs/karate","slug":"port-must-be-between-1-and-65535","errorCode":null,"errorMessage":"port must be between 1 and 65535","messagePattern":"port must be between 1 and 65535","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/driver/cdp/CdpLauncher.java","lineNumber":137,"sourceCode":"        launcher.webSocketUrl = launcher.waitForWebSocketUrl(timeout);\n        if (launcher.webSocketUrl == null) {\n            launcher.close();\n            throw new RuntimeException(\"chrome failed to start or no page targets available within timeout (\" + timeout + \"ms)\");\n        }\n\n        logger.info(\"chrome started on port {} with WebSocket: {}\", port, launcher.webSocketUrl);\n        return launcher;\n    }\n\n    /**\n     * Get WebSocket URL from existing browser at host:port.\n     */\n    public static String getWebSocketUrl(String host, int port) {\n        if (host == null || host.isEmpty()) {\n            host = \"localhost\";\n        }\n        if (port <= 0 || port > 65535) {\n            throw new IllegalArgumentException(\"port must be between 1 and 65535\");\n        }\n        return fetchWebSocketUrl(host, port);\n    }\n\n    private static String resolveExecutable(String configured) {\n        if (configured != null && !configured.isEmpty()) {\n            if (Files.isExecutable(Path.of(configured))) {\n                return configured;\n            }\n            logger.warn(\"configured executable not found: {}\", configured);\n        }\n\n        String defaultPath = getDefaultPath();\n        if (defaultPath != null && Files.isExecutable(Path.of(defaultPath))) {\n            logger.debug(\"using default chrome path: {}\", defaultPath);\n            return defaultPath;\n        }\n","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/driver/cdp/CdpLauncher.java#L119-L155","documentation":"getWebSocketUrl(host, port) validates the DevTools port range before making an HTTP request to http://host:port. Ports outside 1-65535 are invalid, so an IllegalArgumentException is thrown up front instead of a confusing network failure later.","triggerScenarios":"Calling CdpLauncher.getWebSocketUrl(host, 0), a negative port, or a port above 65535; passing an uninitialized port variable that defaulted to 0.","commonSituations":"Port field defaulting to 0 ('auto') and then passed directly to this API; parsing a port string incorrectly; mixing up a local socket port with an unrelated numeric setting.","solutions":["Pass a real DevTools port in 1-65535, or resolve one first with PortUtils.findFreePort()","Validate/normalize the port value at the call site before calling getWebSocketUrl","If the port was configured as 0 meaning 'auto', use CdpLauncher.start(options) instead, which picks a free port itself"],"exampleFix":"// before\nString url = CdpLauncher.getWebSocketUrl(\"localhost\", 0);\n// after\nint port = PortUtils.findFreePort();\nString url = CdpLauncher.getWebSocketUrl(\"localhost\", port);","handlingStrategy":"validation","validationCode":"if (port < 1 || port > 65535) {\n    port = PortUtils.findFreePort();\n}\nCdpLauncher.getWebSocketUrl(host, port);","typeGuard":"Integer validPort(Integer p) {\n    return (p != null && p >= 1 && p <= 65535) ? p : PortUtils.findFreePort();\n}","tryCatchPattern":"try {\n    CdpLauncher.getWebSocketUrl(host, port);\n} catch (IllegalArgumentException e) {\n    // resolve a real port and retry\n    CdpLauncher.getWebSocketUrl(host, PortUtils.findFreePort());\n}","preventionTips":["Never pass a 0-default ('auto') port directly to getWebSocketUrl","Resolve free ports via PortUtils.findFreePort()","Parse configured ports with explicit validation and fail early at config load"],"tags":["port","validation","devtools"],"backgroundTag":"argument-out-of-range","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}