Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error while Intercepting failed requests #2734

Open
2 tasks done
Bingonymous opened this issue Sep 17, 2024 · 5 comments
Open
2 tasks done

Error while Intercepting failed requests #2734

Bingonymous opened this issue Sep 17, 2024 · 5 comments

Comments

@Bingonymous
Copy link

General Troubleshooting

  • I have checked for similar issues on the Issue-tracker.
  • I have checked for PRs that might already address this issue.

Version of JDA

v5.1.0

Expected Behaviour

I try to log all requests from JDA to discord because I get sometimes a globalban (from cloudflare).
They say that they count "401", "403" and "429" errors against me, so I wrote the following code... (see code example)

Code Example for Reproduction Steps

Interceptor interceptor = new Interceptor() {
    @NotNull
    @Override
    public Response intercept(@NotNull Chain chain) throws IOException {
	Response response = chain.proceed(chain.request());

	int statusCode = response.code();
	if (statusCode == 401 || statusCode == 403 || statusCode == 429) {
            System.out.println("HTTP Status Code: " + statusCode);
            System.out.println("URL: " + response.request().url());
        }

        return response;
    }
};


final JDA core = core = JDABuilder
    .create(getToken(), getEnableIntents())
    .setHttpClientBuilder(IOUtil.newHttpClientBuilder().addInterceptor(interceptor))
    .build();

Code for JDABuilder or DefaultShardManagerBuilder used

final JDA core = core = JDABuilder
    .create(getToken(), getEnableIntents())
    .setHttpClientBuilder(IOUtil.newHttpClientBuilder().addInterceptor(interceptor))
    .build();

Exception or Error

java.lang.IllegalStateException: An error occurred while parsing the response for a RestAction
        at net.dv8tion.jda.api.requests.Response.<init>(Response.java:81)
        at net.dv8tion.jda.api.requests.Response.<init>(Response.java:92)
        at net.dv8tion.jda.internal.requests.Requester$WorkTask.handleResponse(Requester.java:442)
        at net.dv8tion.jda.internal.requests.Requester$WorkTask.access$300(Requester.java:378)
        at net.dv8tion.jda.internal.requests.Requester.execute(Requester.java:245)
        at net.dv8tion.jda.internal.requests.Requester.execute(Requester.java:157)
        at net.dv8tion.jda.internal.requests.Requester.execute(Requester.java:140)
        at net.dv8tion.jda.internal.requests.Requester$WorkTask.execute(Requester.java:406)
        at net.dv8tion.jda.api.requests.SequentialRestRateLimiter$Bucket.execute(SequentialRestRateLimiter.java:477)
        at net.dv8tion.jda.api.requests.SequentialRestRateLimiter$Bucket.run(SequentialRestRateLimiter.java:517)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
        at java.base/java.lang.Thread.run(Thread.java:1570)
Caused by: java.io.IOException: closed
        at okio.RealBufferedSource$inputStream$1.read(RealBufferedSource.kt:157)
        at java.base/java.io.BufferedInputStream.fill(BufferedInputStream.java:291)
        at java.base/java.io.BufferedInputStream.implRead(BufferedInputStream.java:325)
        at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:312)
        at java.base/java.util.zip.CheckedInputStream.read(CheckedInputStream.java:59)
        at java.base/java.util.zip.GZIPInputStream.readUByte(GZIPInputStream.java:277)
        at java.base/java.util.zip.GZIPInputStream.readUShort(GZIPInputStream.java:269)
        at java.base/java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:175)
        at java.base/java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:79)
        at java.base/java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:91)
        at net.dv8tion.jda.internal.utils.IOUtil.getBody(IOUtil.java:279)
        at net.dv8tion.jda.api.requests.Response.<init>(Response.java:77)
        ... 12 common frames omitted
@freya022
Copy link
Contributor

Can't repro, what request causes that? what does IOUtil.newHttpClientBuilder() do?

@Bingonymous
Copy link
Author

Bingonymous commented Sep 17, 2024

IOUtil is from JDA - the error seems to occur only sometimes for some reason

@Bingonymous
Copy link
Author

net.dv8tion.jda.internal.utils.IOUtil

@freya022
Copy link
Contributor

You can also listen for HttpRequestEvent

@Andre601
Copy link
Contributor

This is a guess on my end, but given it has "closed" as the IOException reason and that it mentions InputStreams, maybe it's your call of chain.proceed(...) to cause the InputStream to be closed prematurely?
Whatever it is, something closes the connection/InputStream before JDA is done. At least that's what I understand from the exception given.

On another note, you receiving errors from Cloudflare, with one of them being 429 (Rate limited), is a rather odd issue you seem to have.
Do you by any chance have other bots/services contact the Discord API? Or do you use the bot on a shared host or similar? Because I doubt CF has such strict rate limits in place here and such errors could usually only mean that the same IP creates a ton of requests here.
(Again, guessing here on my end)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants