github-api icon indicating copy to clipboard operation
github-api copied to clipboard

URISyntaxException when I search in repository

Open turbanoff opened this issue 1 year ago • 1 comments

Describe the bug I noticed that search in repositories is unreliable sometimes and could fail with URISyntaxException. Example:

Exception in thread "main" org.kohsuke.github.HttpException: Server returned HTTP response code: -1, message: 'null' for URL: https://api.github.com/repositories/376556942/contents/tasks/]?ref=bbdc0107c309ee7c340b99d537b0db460e30bdf4
	at org.kohsuke.github.GitHubClient.interpretApiError(GitHubClient.java:596)
	at org.kohsuke.github.GitHubClient.sendRequest(GitHubClient.java:449)
	at org.kohsuke.github.GitHubClient.sendRequest(GitHubClient.java:403)
	at org.kohsuke.github.Requester.fetchInto(Requester.java:102)
	at org.kohsuke.github.GHContent.refresh(GHContent.java:423)
	at org.kohsuke.github.Refreshable.refresh(Refreshable.java:30)
	at org.kohsuke.github.GHContent.read(GHContent.java:188)
	at org.christmas.token.GithubApi.main(GithubApi.java:21)
Caused by: java.io.IOException: Invalid URL
	at org.kohsuke.github.extras.HttpClientGitHubConnector.send(HttpClientGitHubConnector.java:53)
	at org.kohsuke.github.GitHubClient.sendRequest(GitHubClient.java:432)
	... 6 more
Caused by: java.net.URISyntaxException: Illegal character in path at index 61: https://api.github.com/repositories/376556942/contents/tasks/]?ref=bbdc0107c309ee7c340b99d537b0db460e30bdf4
	at java.base/java.net.URI$Parser.fail(URI.java:2974)
	at java.base/java.net.URI$Parser.checkChars(URI.java:3145)
	at java.base/java.net.URI$Parser.parseHierarchical(URI.java:3227)
	at java.base/java.net.URI$Parser.parse(URI.java:3175)
	at java.base/java.net.URI.<init>(URI.java:623)
	at java.base/java.net.URL.toURI(URL.java:1056)
	at org.kohsuke.github.extras.HttpClientGitHubConnector.send(HttpClientGitHubConnector.java:51)
	... 7 more

To Reproduce Steps to reproduce the behavior: Run following program:

    public static void main(String[] args) throws Exception {
        GitHub gitHub = GitHub.connectUsingOAuth(GITHUB_TOKEN);
        PagedSearchIterable<GHContent> iterable = gitHub.searchContent()
                .q("print")
                .repo("worlddeleteRin/vkproj")
                .list();
        for (GHContent content : iterable) {
            try (InputStream read = content.read()) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                read.transferTo(baos);
                System.out.println(baos.toString(StandardCharsets.UTF_8));
            }
        }
    }

and check errors in the output

Expected behavior Library could handle unexpected symbols in URLs better. Escape them?

turbanoff avatar Dec 18 '22 09:12 turbanoff

PR's welcome.

bitwiseman avatar Jan 25 '23 17:01 bitwiseman