From 934020c39639f232ef81913a0dea4558016b7de4 Mon Sep 17 00:00:00 2001 From: Tokuhiro Matsuno Date: Fri, 17 Nov 2023 13:32:59 +0900 Subject: [PATCH] support sentMessages in MessagingApiClientException (#1159) --- .../client/MessagingApiClientException.java | 15 +++- .../client/MessagingApiExceptionBuilder.java | 2 +- .../MessagingApiClientExceptionTest.java | 75 +++++++++++++++++++ 3 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 clients/line-bot-messaging-api-client/src/test/java/com/linecorp/bot/messaging/client/MessagingApiClientExceptionTest.java diff --git a/clients/line-bot-messaging-api-client/src/main/java/com/linecorp/bot/messaging/client/MessagingApiClientException.java b/clients/line-bot-messaging-api-client/src/main/java/com/linecorp/bot/messaging/client/MessagingApiClientException.java index 2bf7c3a22..37ee3ed9d 100644 --- a/clients/line-bot-messaging-api-client/src/main/java/com/linecorp/bot/messaging/client/MessagingApiClientException.java +++ b/clients/line-bot-messaging-api-client/src/main/java/com/linecorp/bot/messaging/client/MessagingApiClientException.java @@ -21,6 +21,7 @@ import com.linecorp.bot.client.base.exception.AbstractLineClientException; import com.linecorp.bot.messaging.model.ErrorDetail; +import com.linecorp.bot.messaging.model.SentMessage; import okhttp3.Response; @@ -39,10 +40,16 @@ public class MessagingApiClientException extends AbstractLineClientException { */ private final List details; - public MessagingApiClientException(Response response, String error, List details) { - super(response, "error='" + error + "' details='" + details + "'"); + /** + * Array of sent messages. + */ + private final List sentMessages; + + public MessagingApiClientException(Response response, String error, List details, List sentMessages) { + super(response, "error='" + error + "' details='" + details + "' sentMessages='" + sentMessages + "'"); this.error = error; this.details = details; + this.sentMessages = sentMessages; } public String getError() { @@ -52,4 +59,8 @@ public String getError() { public List getDetails() { return Collections.unmodifiableList(details); } + + public List getSentMessages() { + return Collections.unmodifiableList(sentMessages); + } } diff --git a/clients/line-bot-messaging-api-client/src/main/java/com/linecorp/bot/messaging/client/MessagingApiExceptionBuilder.java b/clients/line-bot-messaging-api-client/src/main/java/com/linecorp/bot/messaging/client/MessagingApiExceptionBuilder.java index d3773dc0a..b61fd9d13 100644 --- a/clients/line-bot-messaging-api-client/src/main/java/com/linecorp/bot/messaging/client/MessagingApiExceptionBuilder.java +++ b/clients/line-bot-messaging-api-client/src/main/java/com/linecorp/bot/messaging/client/MessagingApiExceptionBuilder.java @@ -32,6 +32,6 @@ public MessagingApiExceptionBuilder() { protected IOException buildException(Response response, ErrorResponse errorBody) { return new MessagingApiClientException( response, - errorBody.message(), errorBody.details()); + errorBody.message(), errorBody.details(), errorBody.sentMessages()); } } diff --git a/clients/line-bot-messaging-api-client/src/test/java/com/linecorp/bot/messaging/client/MessagingApiClientExceptionTest.java b/clients/line-bot-messaging-api-client/src/test/java/com/linecorp/bot/messaging/client/MessagingApiClientExceptionTest.java new file mode 100644 index 000000000..457c9f6c7 --- /dev/null +++ b/clients/line-bot-messaging-api-client/src/test/java/com/linecorp/bot/messaging/client/MessagingApiClientExceptionTest.java @@ -0,0 +1,75 @@ +/* + * Copyright 2023 LINE Corporation + * + * LINE Corporation licenses this file to you 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 com.linecorp.bot.messaging.client; + +import okhttp3.Headers; +import okhttp3.Request; +import okhttp3.Response; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.linecorp.bot.messaging.model.ErrorDetail; +import com.linecorp.bot.messaging.model.SentMessage; + +public class MessagingApiClientExceptionTest { + + private Response response; + + @BeforeEach + void setUp() { + response = mock(Response.class); + Request request = new Request.Builder().url("https://example.com/").build(); + + when(response.request()).thenReturn(request); + when(response.code()).thenReturn(200); + + Headers headers = Headers.of( + "x-line-example", "headerValue", + "x-line-request-id", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" + ); + when(response.headers()).thenReturn(headers); + } + + @Test + void constructAndGetTest() { + List errorDetails = Collections.singletonList(new ErrorDetail("message", "property")); + List sentMessages = Collections.singletonList(new SentMessage("IDIDID", "QUOQUOQUO")); + String error = "testError"; + + MessagingApiClientException exception = + new MessagingApiClientException(response, error, errorDetails, sentMessages); + + assertThat(exception).isNotNull(); + assertThat(exception.getMessage()).isEqualTo( + "API returns error: code=" + response.code() + + " requestUrl=" + response.request().url() + + " x-line-example=headerValue x-line-request-id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx " + + "error='testError' " + + "details='[ErrorDetail[message=message, property=property]]'" + + " sentMessages='[SentMessage[id=IDIDID, quoteToken=QUOQUOQUO]]'"); + assertThat(exception.getError()).isEqualTo(error); + assertThat(exception.getDetails()).isEqualTo(errorDetails); + assertThat(exception.getSentMessages()).isEqualTo(sentMessages); + } +}