{"record":{"id":"5bc1a19e1de6b62e","repo":"alibaba/nacos","slug":"invalid-uri-url","errorCode":null,"errorMessage":"Invalid URI [{url}]","messagePattern":"Invalid URI \\[(.+?)\\]","errorType":"exception","errorClass":"NestedIoException","httpStatus":null,"severity":"error","filePath":"common/src/main/java/com/alibaba/nacos/common/packagescan/resource/AbstractResource.java","lineNumber":124,"sourceCode":"     * This implementation throws a FileNotFoundException, assuming\n     * that the resource cannot be resolved to a URL.\n     */\n    @Override\n    public URL getUrl() throws IOException {\n        throw new FileNotFoundException(getDescription() + \" cannot be resolved to URL\");\n    }\n\n    /**\n     * This implementation builds a URI based on the URL returned\n     * by {@link #getUrl()}.\n     */\n    @Override\n    public URI getUri() throws IOException {\n        URL url = getUrl();\n        try {\n            return ResourceUtils.toUri(url);\n        } catch (URISyntaxException ex) {\n            throw new NestedIoException(\"Invalid URI [\" + url + \"]\", ex);\n        }\n    }\n\n    /**\n     * This implementation throws a FileNotFoundException, assuming\n     * that the resource cannot be resolved to an absolute file path.\n     */\n    @Override\n    public File getFile() throws IOException {\n        throw new FileNotFoundException(getDescription() + \" cannot be resolved to absolute file path\");\n    }\n\n    /**\n     * This implementation returns {@link Channels#newChannel(InputStream)}\n     * with the result of {@link #getInputStream()}.\n     * This is the same as in {@link Resource}'s corresponding default method\n     * but mirrored here for efficient JVM-level dispatching in a class hierarchy.\n     */","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/alibaba/nacos/blob/9b989acdf181d00898f2e8839257bb2b2a3cefe3/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/AbstractResource.java#L106-L142","documentation":"Thrown by AbstractResource.getUri() when getUrl() succeeds but ResourceUtils.toUri(url) raises URISyntaxException — the URL string is not a valid URI. Wrapped as NestedIoException. It indicates a malformed URL (illegal characters, bad encoding) rather than a missing resource.","triggerScenarios":"Calling resource.getUri() on a resource whose URL contains characters illegal in a URI (spaces, unencoded non-ASCII, stray characters) so java.net.URI rejects it.","commonSituations":"File paths with spaces or non-ASCII on Windows; custom URL schemes with unencoded components; a classpath URL containing characters that survive as URL but fail URI parsing; a misconstructed resource path.","solutions":["URL-encode path components when constructing the resource URL so getUri() parses cleanly.","Catch NestedIoException/URISyntaxException and fall back to using the URL directly instead of URI.","Sanitize the path (remove/encode spaces) before creating the resource.","Use ResourceUtils.toUri after pre-encoding with URLEncoder on the relevant segment."],"exampleFix":"// before\nResource res = new UrlResource(\"file:/C:/Program Files/app/conf.json\");\nURI u = res.getUri(); // NestedIoException: Invalid URI (space)\n\n// after\nString encoded = \"file:///C:/Program%20Files/app/conf.json\";\nResource res = new UrlResource(encoded);\nURI u = res.getUri();","handlingStrategy":"validation","validationCode":"// pre-encode path segments containing illegal URI characters\nString safe = rawPath.replace(\" \", \"%20\");\nResource res = new UrlResource(safe);\nURI u = res.getUri();","typeGuard":"boolean isParsableUri(URL url) {\n    try { url.toURI(); return true; }\n    catch (URISyntaxException e) { return false; }\n}","tryCatchPattern":"try {\n    URI u = resource.getUri();\n} catch (NestedIoException e) {\n    if (e.getCause() instanceof URISyntaxException) {\n        log.error(\"Malformed URL — encode path components: {}\", e.getMessage());\n    }\n    throw e;\n}","preventionTips":["URL-encode spaces and non-ASCII in file paths, especially on Windows.","Validate URLs with new URI(spec) before constructing resources.","Catch NestedIoException and fall back to the URL when a URI is unavailable."],"tags":["resource","io","url","uri","encoding"],"backgroundTag":null,"analyzedSha":"9b989acdf181d00898f2e8839257bb2b2a3cefe3","analyzedAt":"2026-08-14T07:17:31.569Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}