From 0918871faa992f5ac6ee91c3433f16b4d1f26503 Mon Sep 17 00:00:00 2001 From: freya02 <41875020+freya022@users.noreply.github.com> Date: Thu, 22 Feb 2024 17:38:57 +0100 Subject: [PATCH 1/6] Create an exception when receiving UNKNOWN_WEBHOOK in interaction hooks --- .../net/dv8tion/jda/api/requests/Request.java | 18 +++++++++-- .../internal/entities/ReceivedMessage.java | 30 +++++++++++++++---- .../jda/internal/requests/RestActionImpl.java | 9 +++++- .../restaction/MessageEditActionImpl.java | 12 ++++++++ 4 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/api/requests/Request.java b/src/main/java/net/dv8tion/jda/api/requests/Request.java index 468a2df017..8eb2d27a68 100644 --- a/src/main/java/net/dv8tion/jda/api/requests/Request.java +++ b/src/main/java/net/dv8tion/jda/api/requests/Request.java @@ -138,15 +138,27 @@ public void onFailure(Response response) { if (response.code == 429) { - onFailure(new RateLimitedException(route, response.retryAfter)); + onFailure(createRateLimitedException(response)); } else { - onFailure(ErrorResponseException.create( - ErrorResponse.fromJSON(response.optObject().orElse(null)), response)); + onFailure(createErrorResponseException(response)); } } + @Nonnull + public RateLimitedException createRateLimitedException(@Nonnull Response response) + { + return new RateLimitedException(route, response.retryAfter); + } + + @Nonnull + public ErrorResponseException createErrorResponseException(@Nonnull Response response) + { + return ErrorResponseException.create( + ErrorResponse.fromJSON(response.optObject().orElse(null)), response); + } + public void onFailure(Throwable failException) { if (done) diff --git a/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java b/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java index 4d5dab6962..9ba24f4e14 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java @@ -34,14 +34,13 @@ import net.dv8tion.jda.api.entities.emoji.Emoji; import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji; import net.dv8tion.jda.api.entities.sticker.StickerItem; +import net.dv8tion.jda.api.exceptions.ErrorResponseException; import net.dv8tion.jda.api.exceptions.InsufficientPermissionException; import net.dv8tion.jda.api.exceptions.PermissionException; import net.dv8tion.jda.api.interactions.InteractionHook; import net.dv8tion.jda.api.interactions.components.ActionRow; import net.dv8tion.jda.api.interactions.components.LayoutComponent; -import net.dv8tion.jda.api.requests.GatewayIntent; -import net.dv8tion.jda.api.requests.RestAction; -import net.dv8tion.jda.api.requests.Route; +import net.dv8tion.jda.api.requests.*; import net.dv8tion.jda.api.requests.restaction.AuditableRestAction; import net.dv8tion.jda.api.requests.restaction.MessageEditAction; import net.dv8tion.jda.api.requests.restaction.ThreadChannelAction; @@ -51,6 +50,7 @@ import net.dv8tion.jda.api.utils.data.DataObject; import net.dv8tion.jda.api.utils.messages.MessageEditData; import net.dv8tion.jda.internal.JDAImpl; +import net.dv8tion.jda.internal.interactions.InteractionHookImpl; import net.dv8tion.jda.internal.requests.CompletedRestAction; import net.dv8tion.jda.internal.requests.RestActionImpl; import net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl; @@ -774,7 +774,17 @@ public AuditableRestAction delete() if (isWebhookRequest()) { Route.CompiledRoute route = Route.Webhooks.EXECUTE_WEBHOOK_DELETE.compile(webhook.getId(), webhook.getToken(), getId()); - return new AuditableRestActionImpl<>(getJDA(), route); + return new AuditableRestActionImpl(getJDA(), route) + { + @Override + protected void handleErrorResponse(Response response, Request request, ErrorResponseException exception) + { + if (webhook instanceof InteractionHookImpl && exception.getErrorResponse() == ErrorResponse.UNKNOWN_WEBHOOK) + request.onFailure(new IllegalStateException("Sending a webhook request requires the interaction to be acknowledged before expiration")); + else + super.handleErrorResponse(response, request, exception); + } + }; } SelfUser self = getJDA().getSelfUser(); @@ -839,7 +849,17 @@ public AuditableRestAction suppressEmbeds(boolean suppressed) newFlags &= ~suppressionValue; DataObject body = DataObject.empty().put("flags", newFlags); - return new AuditableRestActionImpl<>(api, route, body); + return new AuditableRestActionImpl(api, route, body) + { + @Override + protected void handleErrorResponse(Response response, Request request, ErrorResponseException exception) + { + if (webhook instanceof InteractionHookImpl && exception.getErrorResponse() == ErrorResponse.UNKNOWN_WEBHOOK) + request.onFailure(new IllegalStateException("Sending a webhook request requires the interaction to be acknowledged before expiration")); + else + super.handleErrorResponse(response, request, exception); + } + }; } @Nonnull diff --git a/src/main/java/net/dv8tion/jda/internal/requests/RestActionImpl.java b/src/main/java/net/dv8tion/jda/internal/requests/RestActionImpl.java index 3184d67d1c..55306558be 100644 --- a/src/main/java/net/dv8tion/jda/internal/requests/RestActionImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/requests/RestActionImpl.java @@ -277,8 +277,10 @@ public void handleResponse(Response response, Request request) { if (response.isOk()) handleSuccess(response, request); + else if (response.isRateLimit()) + request.onFailure(request.createRateLimitedException(response)); else - request.onFailure(response); + handleErrorResponse(response, request, request.createErrorResponseException(response)); } protected void handleSuccess(Response response, Request request) @@ -289,6 +291,11 @@ protected void handleSuccess(Response response, Request request) request.onSuccess(handler.apply(response, request)); } + protected void handleErrorResponse(Response response, Request request, ErrorResponseException exception) + { + request.onFailure(exception); + } + private long getDeadline() { return deadline > 0 diff --git a/src/main/java/net/dv8tion/jda/internal/requests/restaction/MessageEditActionImpl.java b/src/main/java/net/dv8tion/jda/internal/requests/restaction/MessageEditActionImpl.java index d82cb08460..e020be7c83 100644 --- a/src/main/java/net/dv8tion/jda/internal/requests/restaction/MessageEditActionImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/requests/restaction/MessageEditActionImpl.java @@ -22,7 +22,9 @@ import net.dv8tion.jda.api.entities.WebhookClient; import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +import net.dv8tion.jda.api.exceptions.ErrorResponseException; import net.dv8tion.jda.api.interactions.InteractionHook; +import net.dv8tion.jda.api.requests.ErrorResponse; import net.dv8tion.jda.api.requests.Request; import net.dv8tion.jda.api.requests.Response; import net.dv8tion.jda.api.requests.Route; @@ -32,6 +34,7 @@ import net.dv8tion.jda.api.utils.messages.MessageEditData; import net.dv8tion.jda.internal.entities.EntityBuilder; import net.dv8tion.jda.internal.entities.ReceivedMessage; +import net.dv8tion.jda.internal.interactions.InteractionHookImpl; import net.dv8tion.jda.internal.requests.RestActionImpl; import net.dv8tion.jda.internal.utils.message.MessageEditBuilderMixin; import okhttp3.RequestBody; @@ -103,6 +106,15 @@ protected void handleSuccess(Response response, Request request) request.onSuccess(message.withHook(webhook)); } + @Override + protected void handleErrorResponse(Response response, Request request, ErrorResponseException exception) + { + if (webhook instanceof InteractionHookImpl && exception.getErrorResponse() == ErrorResponse.UNKNOWN_WEBHOOK) + request.onFailure(new IllegalStateException("Sending a webhook request requires the interaction to be acknowledged before expiration")); + else + super.handleErrorResponse(response, request, exception); + } + @Nonnull @Override public MessageEditAction setCheck(BooleanSupplier checks) From 638c407326e7cfe51ae4913f0752e2e256f0ae86 Mon Sep 17 00:00:00 2001 From: freya02 <41875020+freya022@users.noreply.github.com> Date: Thu, 22 Feb 2024 17:43:36 +0100 Subject: [PATCH 2/6] Check interactions are still not acknowledged --- .../dv8tion/jda/internal/entities/ReceivedMessage.java | 8 ++++++-- .../requests/restaction/MessageEditActionImpl.java | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java b/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java index 9ba24f4e14..03b1a5c05a 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java @@ -779,7 +779,9 @@ public AuditableRestAction delete() @Override protected void handleErrorResponse(Response response, Request request, ErrorResponseException exception) { - if (webhook instanceof InteractionHookImpl && exception.getErrorResponse() == ErrorResponse.UNKNOWN_WEBHOOK) + if (webhook instanceof InteractionHookImpl + && !((InteractionHookImpl) webhook).isAck() + && exception.getErrorResponse() == ErrorResponse.UNKNOWN_WEBHOOK) request.onFailure(new IllegalStateException("Sending a webhook request requires the interaction to be acknowledged before expiration")); else super.handleErrorResponse(response, request, exception); @@ -854,7 +856,9 @@ public AuditableRestAction suppressEmbeds(boolean suppressed) @Override protected void handleErrorResponse(Response response, Request request, ErrorResponseException exception) { - if (webhook instanceof InteractionHookImpl && exception.getErrorResponse() == ErrorResponse.UNKNOWN_WEBHOOK) + if (webhook instanceof InteractionHookImpl + && !((InteractionHookImpl) webhook).isAck() + && exception.getErrorResponse() == ErrorResponse.UNKNOWN_WEBHOOK) request.onFailure(new IllegalStateException("Sending a webhook request requires the interaction to be acknowledged before expiration")); else super.handleErrorResponse(response, request, exception); diff --git a/src/main/java/net/dv8tion/jda/internal/requests/restaction/MessageEditActionImpl.java b/src/main/java/net/dv8tion/jda/internal/requests/restaction/MessageEditActionImpl.java index e020be7c83..cfe715d43b 100644 --- a/src/main/java/net/dv8tion/jda/internal/requests/restaction/MessageEditActionImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/requests/restaction/MessageEditActionImpl.java @@ -109,7 +109,9 @@ protected void handleSuccess(Response response, Request request) @Override protected void handleErrorResponse(Response response, Request request, ErrorResponseException exception) { - if (webhook instanceof InteractionHookImpl && exception.getErrorResponse() == ErrorResponse.UNKNOWN_WEBHOOK) + if (webhook instanceof InteractionHookImpl + && !((InteractionHookImpl) webhook).isAck() + && exception.getErrorResponse() == ErrorResponse.UNKNOWN_WEBHOOK) request.onFailure(new IllegalStateException("Sending a webhook request requires the interaction to be acknowledged before expiration")); else super.handleErrorResponse(response, request, exception); From 73438317c291e9ccf7607999a4933911a345e21f Mon Sep 17 00:00:00 2001 From: freya02 <41875020+freya022@users.noreply.github.com> Date: Mon, 26 Feb 2024 20:04:27 +0100 Subject: [PATCH 3/6] Revert ratelimit handling changes --- src/main/java/net/dv8tion/jda/api/requests/Request.java | 7 +++---- .../net/dv8tion/jda/internal/requests/RestActionImpl.java | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/api/requests/Request.java b/src/main/java/net/dv8tion/jda/api/requests/Request.java index 8eb2d27a68..cdb4117111 100644 --- a/src/main/java/net/dv8tion/jda/api/requests/Request.java +++ b/src/main/java/net/dv8tion/jda/api/requests/Request.java @@ -138,7 +138,7 @@ public void onFailure(Response response) { if (response.code == 429) { - onFailure(createRateLimitedException(response)); + onRateLimited(response); } else { @@ -146,10 +146,9 @@ public void onFailure(Response response) } } - @Nonnull - public RateLimitedException createRateLimitedException(@Nonnull Response response) + public void onRateLimited(Response response) { - return new RateLimitedException(route, response.retryAfter); + onFailure(new RateLimitedException(route, response.retryAfter)); } @Nonnull diff --git a/src/main/java/net/dv8tion/jda/internal/requests/RestActionImpl.java b/src/main/java/net/dv8tion/jda/internal/requests/RestActionImpl.java index 55306558be..92e03d0fb9 100644 --- a/src/main/java/net/dv8tion/jda/internal/requests/RestActionImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/requests/RestActionImpl.java @@ -278,7 +278,7 @@ public void handleResponse(Response response, Request request) if (response.isOk()) handleSuccess(response, request); else if (response.isRateLimit()) - request.onFailure(request.createRateLimitedException(response)); + request.onRateLimited(response); else handleErrorResponse(response, request, request.createErrorResponseException(response)); } From 7e9c821eb2bbfc5a46d3240dd38bca6efdc029ae Mon Sep 17 00:00:00 2001 From: freya02 <41875020+freya022@users.noreply.github.com> Date: Mon, 26 Feb 2024 20:06:10 +0100 Subject: [PATCH 4/6] Replace RestAction error response handling with an ErrorMapper --- .../internal/entities/ReceivedMessage.java | 53 +++++++++---------- .../jda/internal/requests/ErrorMapper.java | 31 +++++++++++ .../jda/internal/requests/RestActionImpl.java | 25 ++++++--- .../restaction/AuditableRestActionImpl.java | 8 +++ .../restaction/MessageEditActionImpl.java | 14 ++--- 5 files changed, 88 insertions(+), 43 deletions(-) create mode 100644 src/main/java/net/dv8tion/jda/internal/requests/ErrorMapper.java diff --git a/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java b/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java index 03b1a5c05a..74cfb24b5b 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java @@ -34,13 +34,15 @@ import net.dv8tion.jda.api.entities.emoji.Emoji; import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji; import net.dv8tion.jda.api.entities.sticker.StickerItem; -import net.dv8tion.jda.api.exceptions.ErrorResponseException; import net.dv8tion.jda.api.exceptions.InsufficientPermissionException; import net.dv8tion.jda.api.exceptions.PermissionException; import net.dv8tion.jda.api.interactions.InteractionHook; import net.dv8tion.jda.api.interactions.components.ActionRow; import net.dv8tion.jda.api.interactions.components.LayoutComponent; -import net.dv8tion.jda.api.requests.*; +import net.dv8tion.jda.api.requests.ErrorResponse; +import net.dv8tion.jda.api.requests.GatewayIntent; +import net.dv8tion.jda.api.requests.RestAction; +import net.dv8tion.jda.api.requests.Route; import net.dv8tion.jda.api.requests.restaction.AuditableRestAction; import net.dv8tion.jda.api.requests.restaction.MessageEditAction; import net.dv8tion.jda.api.requests.restaction.ThreadChannelAction; @@ -52,6 +54,7 @@ import net.dv8tion.jda.internal.JDAImpl; import net.dv8tion.jda.internal.interactions.InteractionHookImpl; import net.dv8tion.jda.internal.requests.CompletedRestAction; +import net.dv8tion.jda.internal.requests.ErrorMapper; import net.dv8tion.jda.internal.requests.RestActionImpl; import net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl; import net.dv8tion.jda.internal.requests.restaction.MessageEditActionImpl; @@ -775,18 +778,7 @@ public AuditableRestAction delete() { Route.CompiledRoute route = Route.Webhooks.EXECUTE_WEBHOOK_DELETE.compile(webhook.getId(), webhook.getToken(), getId()); return new AuditableRestActionImpl(getJDA(), route) - { - @Override - protected void handleErrorResponse(Response response, Request request, ErrorResponseException exception) - { - if (webhook instanceof InteractionHookImpl - && !((InteractionHookImpl) webhook).isAck() - && exception.getErrorResponse() == ErrorResponse.UNKNOWN_WEBHOOK) - request.onFailure(new IllegalStateException("Sending a webhook request requires the interaction to be acknowledged before expiration")); - else - super.handleErrorResponse(response, request, exception); - } - }; + .setErrorMapper(getUnknownWebhookErrorMapper()); } SelfUser self = getJDA().getSelfUser(); @@ -852,18 +844,7 @@ public AuditableRestAction suppressEmbeds(boolean suppressed) DataObject body = DataObject.empty().put("flags", newFlags); return new AuditableRestActionImpl(api, route, body) - { - @Override - protected void handleErrorResponse(Response response, Request request, ErrorResponseException exception) - { - if (webhook instanceof InteractionHookImpl - && !((InteractionHookImpl) webhook).isAck() - && exception.getErrorResponse() == ErrorResponse.UNKNOWN_WEBHOOK) - request.onFailure(new IllegalStateException("Sending a webhook request requires the interaction to be acknowledged before expiration")); - else - super.handleErrorResponse(response, request, exception); - } - }; + .setErrorMapper(getUnknownWebhookErrorMapper()); } @Nonnull @@ -1004,8 +985,26 @@ private boolean isWebhookRequest() @Nonnull private MessageEditActionImpl editRequest() { - return hasChannel() + final MessageEditActionImpl messageEditAction = hasChannel() ? new MessageEditActionImpl(getChannel(), getId()) : new MessageEditActionImpl(getJDA(), hasGuild() ? getGuild() : null, getChannelId(), getId()); + + return messageEditAction.setErrorMapper(getUnknownWebhookErrorMapper()); + } + + private ErrorMapper getUnknownWebhookErrorMapper() + { + if (!isWebhookRequest()) + return null; + + return (response, request, exception) -> + { + if (webhook instanceof InteractionHookImpl + && !((InteractionHookImpl) webhook).isAck() + && exception.getErrorResponse() == ErrorResponse.UNKNOWN_WEBHOOK) + return new IllegalStateException("Sending a webhook request requires the interaction to be acknowledged before expiration"); + else + return null; + }; } } diff --git a/src/main/java/net/dv8tion/jda/internal/requests/ErrorMapper.java b/src/main/java/net/dv8tion/jda/internal/requests/ErrorMapper.java new file mode 100644 index 0000000000..92d785dbac --- /dev/null +++ b/src/main/java/net/dv8tion/jda/internal/requests/ErrorMapper.java @@ -0,0 +1,31 @@ +/* + * Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dv8tion.jda.internal.requests; + +import net.dv8tion.jda.api.exceptions.ErrorResponseException; +import net.dv8tion.jda.api.requests.Request; +import net.dv8tion.jda.api.requests.Response; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +@FunctionalInterface +public interface ErrorMapper +{ + @Nullable + Throwable apply(@Nonnull Response response, @Nonnull Request request, @Nonnull ErrorResponseException exception); +} diff --git a/src/main/java/net/dv8tion/jda/internal/requests/RestActionImpl.java b/src/main/java/net/dv8tion/jda/internal/requests/RestActionImpl.java index 92e03d0fb9..1dcbc6f234 100644 --- a/src/main/java/net/dv8tion/jda/internal/requests/RestActionImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/requests/RestActionImpl.java @@ -63,6 +63,7 @@ else if (t.getCause() != null) private final Route.CompiledRoute route; private final RequestBody data; private final BiFunction, T> handler; + private ErrorMapper errorMapper = null; private boolean priority = false; private long deadline = 0; @@ -146,6 +147,13 @@ public RestActionImpl(JDA api, Route.CompiledRoute route, RequestBody data, BiFu this.handler = handler; } + @Nonnull + public RestAction setErrorMapper(ErrorMapper errorMapper) + { + this.errorMapper = errorMapper; + return this; + } + public RestActionImpl priority() { priority = true; @@ -280,7 +288,17 @@ public void handleResponse(Response response, Request request) else if (response.isRateLimit()) request.onRateLimited(response); else - handleErrorResponse(response, request, request.createErrorResponseException(response)); + { + final ErrorResponseException exception = request.createErrorResponseException(response); + final Throwable mappedThrowable = this.errorMapper != null + ? this.errorMapper.apply(response, request, exception) + : null; + + if (mappedThrowable != null) + request.onFailure(mappedThrowable); + else + request.onFailure(exception); + } } protected void handleSuccess(Response response, Request request) @@ -291,11 +309,6 @@ protected void handleSuccess(Response response, Request request) request.onSuccess(handler.apply(response, request)); } - protected void handleErrorResponse(Response response, Request request, ErrorResponseException exception) - { - request.onFailure(exception); - } - private long getDeadline() { return deadline > 0 diff --git a/src/main/java/net/dv8tion/jda/internal/requests/restaction/AuditableRestActionImpl.java b/src/main/java/net/dv8tion/jda/internal/requests/restaction/AuditableRestActionImpl.java index 160277f7e3..738c31cd12 100644 --- a/src/main/java/net/dv8tion/jda/internal/requests/restaction/AuditableRestActionImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/requests/restaction/AuditableRestActionImpl.java @@ -23,6 +23,7 @@ import net.dv8tion.jda.api.requests.Route; import net.dv8tion.jda.api.requests.restaction.AuditableRestAction; import net.dv8tion.jda.api.utils.data.DataObject; +import net.dv8tion.jda.internal.requests.ErrorMapper; import net.dv8tion.jda.internal.requests.RestActionImpl; import net.dv8tion.jda.internal.utils.EncodingUtil; import okhttp3.RequestBody; @@ -69,6 +70,13 @@ public AuditableRestActionImpl(JDA api, Route.CompiledRoute route, RequestBody d super(api, route, data, handler); } + @Nonnull + @Override + public AuditableRestAction setErrorMapper(ErrorMapper errorMapper) + { + return (AuditableRestAction) super.setErrorMapper(errorMapper); + } + @Nonnull @Override public AuditableRestAction setCheck(BooleanSupplier checks) diff --git a/src/main/java/net/dv8tion/jda/internal/requests/restaction/MessageEditActionImpl.java b/src/main/java/net/dv8tion/jda/internal/requests/restaction/MessageEditActionImpl.java index cfe715d43b..0608e201a1 100644 --- a/src/main/java/net/dv8tion/jda/internal/requests/restaction/MessageEditActionImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/requests/restaction/MessageEditActionImpl.java @@ -22,9 +22,7 @@ import net.dv8tion.jda.api.entities.WebhookClient; import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; -import net.dv8tion.jda.api.exceptions.ErrorResponseException; import net.dv8tion.jda.api.interactions.InteractionHook; -import net.dv8tion.jda.api.requests.ErrorResponse; import net.dv8tion.jda.api.requests.Request; import net.dv8tion.jda.api.requests.Response; import net.dv8tion.jda.api.requests.Route; @@ -34,7 +32,7 @@ import net.dv8tion.jda.api.utils.messages.MessageEditData; import net.dv8tion.jda.internal.entities.EntityBuilder; import net.dv8tion.jda.internal.entities.ReceivedMessage; -import net.dv8tion.jda.internal.interactions.InteractionHookImpl; +import net.dv8tion.jda.internal.requests.ErrorMapper; import net.dv8tion.jda.internal.requests.RestActionImpl; import net.dv8tion.jda.internal.utils.message.MessageEditBuilderMixin; import okhttp3.RequestBody; @@ -106,15 +104,11 @@ protected void handleSuccess(Response response, Request request) request.onSuccess(message.withHook(webhook)); } + @Nonnull @Override - protected void handleErrorResponse(Response response, Request request, ErrorResponseException exception) + public MessageEditActionImpl setErrorMapper(ErrorMapper errorMapper) { - if (webhook instanceof InteractionHookImpl - && !((InteractionHookImpl) webhook).isAck() - && exception.getErrorResponse() == ErrorResponse.UNKNOWN_WEBHOOK) - request.onFailure(new IllegalStateException("Sending a webhook request requires the interaction to be acknowledged before expiration")); - else - super.handleErrorResponse(response, request, exception); + return (MessageEditActionImpl) super.setErrorMapper(errorMapper); } @Nonnull From 8f24e6a2ba850299ddf5a27569dca7904d194ac5 Mon Sep 17 00:00:00 2001 From: freya02 <41875020+freya022@users.noreply.github.com> Date: Mon, 26 Feb 2024 20:20:49 +0100 Subject: [PATCH 5/6] Don't chain RestActionImpl#setErrorMapper --- .../jda/internal/entities/ReceivedMessage.java | 13 ++++++++----- .../jda/internal/requests/RestActionImpl.java | 4 +--- .../restaction/AuditableRestActionImpl.java | 8 -------- .../requests/restaction/MessageEditActionImpl.java | 8 -------- 4 files changed, 9 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java b/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java index 74cfb24b5b..0223363ab8 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java @@ -777,8 +777,9 @@ public AuditableRestAction delete() if (isWebhookRequest()) { Route.CompiledRoute route = Route.Webhooks.EXECUTE_WEBHOOK_DELETE.compile(webhook.getId(), webhook.getToken(), getId()); - return new AuditableRestActionImpl(getJDA(), route) - .setErrorMapper(getUnknownWebhookErrorMapper()); + final AuditableRestActionImpl action = new AuditableRestActionImpl<>(getJDA(), route); + action.setErrorMapper(getUnknownWebhookErrorMapper()); + return action; } SelfUser self = getJDA().getSelfUser(); @@ -843,8 +844,9 @@ public AuditableRestAction suppressEmbeds(boolean suppressed) newFlags &= ~suppressionValue; DataObject body = DataObject.empty().put("flags", newFlags); - return new AuditableRestActionImpl(api, route, body) - .setErrorMapper(getUnknownWebhookErrorMapper()); + final AuditableRestActionImpl action = new AuditableRestActionImpl<>(api, route, body); + action.setErrorMapper(getUnknownWebhookErrorMapper()); + return action; } @Nonnull @@ -989,7 +991,8 @@ private MessageEditActionImpl editRequest() ? new MessageEditActionImpl(getChannel(), getId()) : new MessageEditActionImpl(getJDA(), hasGuild() ? getGuild() : null, getChannelId(), getId()); - return messageEditAction.setErrorMapper(getUnknownWebhookErrorMapper()); + messageEditAction.setErrorMapper(getUnknownWebhookErrorMapper()); + return messageEditAction; } private ErrorMapper getUnknownWebhookErrorMapper() diff --git a/src/main/java/net/dv8tion/jda/internal/requests/RestActionImpl.java b/src/main/java/net/dv8tion/jda/internal/requests/RestActionImpl.java index 1dcbc6f234..48eb4fb741 100644 --- a/src/main/java/net/dv8tion/jda/internal/requests/RestActionImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/requests/RestActionImpl.java @@ -147,11 +147,9 @@ public RestActionImpl(JDA api, Route.CompiledRoute route, RequestBody data, BiFu this.handler = handler; } - @Nonnull - public RestAction setErrorMapper(ErrorMapper errorMapper) + public void setErrorMapper(ErrorMapper errorMapper) { this.errorMapper = errorMapper; - return this; } public RestActionImpl priority() diff --git a/src/main/java/net/dv8tion/jda/internal/requests/restaction/AuditableRestActionImpl.java b/src/main/java/net/dv8tion/jda/internal/requests/restaction/AuditableRestActionImpl.java index 738c31cd12..160277f7e3 100644 --- a/src/main/java/net/dv8tion/jda/internal/requests/restaction/AuditableRestActionImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/requests/restaction/AuditableRestActionImpl.java @@ -23,7 +23,6 @@ import net.dv8tion.jda.api.requests.Route; import net.dv8tion.jda.api.requests.restaction.AuditableRestAction; import net.dv8tion.jda.api.utils.data.DataObject; -import net.dv8tion.jda.internal.requests.ErrorMapper; import net.dv8tion.jda.internal.requests.RestActionImpl; import net.dv8tion.jda.internal.utils.EncodingUtil; import okhttp3.RequestBody; @@ -70,13 +69,6 @@ public AuditableRestActionImpl(JDA api, Route.CompiledRoute route, RequestBody d super(api, route, data, handler); } - @Nonnull - @Override - public AuditableRestAction setErrorMapper(ErrorMapper errorMapper) - { - return (AuditableRestAction) super.setErrorMapper(errorMapper); - } - @Nonnull @Override public AuditableRestAction setCheck(BooleanSupplier checks) diff --git a/src/main/java/net/dv8tion/jda/internal/requests/restaction/MessageEditActionImpl.java b/src/main/java/net/dv8tion/jda/internal/requests/restaction/MessageEditActionImpl.java index 0608e201a1..d82cb08460 100644 --- a/src/main/java/net/dv8tion/jda/internal/requests/restaction/MessageEditActionImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/requests/restaction/MessageEditActionImpl.java @@ -32,7 +32,6 @@ import net.dv8tion.jda.api.utils.messages.MessageEditData; import net.dv8tion.jda.internal.entities.EntityBuilder; import net.dv8tion.jda.internal.entities.ReceivedMessage; -import net.dv8tion.jda.internal.requests.ErrorMapper; import net.dv8tion.jda.internal.requests.RestActionImpl; import net.dv8tion.jda.internal.utils.message.MessageEditBuilderMixin; import okhttp3.RequestBody; @@ -104,13 +103,6 @@ protected void handleSuccess(Response response, Request request) request.onSuccess(message.withHook(webhook)); } - @Nonnull - @Override - public MessageEditActionImpl setErrorMapper(ErrorMapper errorMapper) - { - return (MessageEditActionImpl) super.setErrorMapper(errorMapper); - } - @Nonnull @Override public MessageEditAction setCheck(BooleanSupplier checks) From 3fab039e3a10362bbb42fcc601a2d0bf666af4cc Mon Sep 17 00:00:00 2001 From: freya02 <41875020+freya022@users.noreply.github.com> Date: Sat, 30 Mar 2024 20:26:12 +0100 Subject: [PATCH 6/6] Set original exception as cause --- .../java/net/dv8tion/jda/internal/entities/ReceivedMessage.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java b/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java index 0223363ab8..d425ab74df 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/ReceivedMessage.java @@ -1005,7 +1005,7 @@ private ErrorMapper getUnknownWebhookErrorMapper() if (webhook instanceof InteractionHookImpl && !((InteractionHookImpl) webhook).isAck() && exception.getErrorResponse() == ErrorResponse.UNKNOWN_WEBHOOK) - return new IllegalStateException("Sending a webhook request requires the interaction to be acknowledged before expiration"); + return new IllegalStateException("Sending a webhook request requires the interaction to be acknowledged before expiration", exception); else return null; };