Skip to content

Commit 934020c

Browse files
authored
support sentMessages in MessagingApiClientException (#1159)
1 parent fc1486f commit 934020c

File tree

3 files changed

+89
-3
lines changed

3 files changed

+89
-3
lines changed

clients/line-bot-messaging-api-client/src/main/java/com/linecorp/bot/messaging/client/MessagingApiClientException.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.linecorp.bot.client.base.exception.AbstractLineClientException;
2323
import com.linecorp.bot.messaging.model.ErrorDetail;
24+
import com.linecorp.bot.messaging.model.SentMessage;
2425

2526
import okhttp3.Response;
2627

@@ -39,10 +40,16 @@ public class MessagingApiClientException extends AbstractLineClientException {
3940
*/
4041
private final List<ErrorDetail> details;
4142

42-
public MessagingApiClientException(Response response, String error, List<ErrorDetail> details) {
43-
super(response, "error='" + error + "' details='" + details + "'");
43+
/**
44+
* Array of sent messages.
45+
*/
46+
private final List<SentMessage> sentMessages;
47+
48+
public MessagingApiClientException(Response response, String error, List<ErrorDetail> details, List<SentMessage> sentMessages) {
49+
super(response, "error='" + error + "' details='" + details + "' sentMessages='" + sentMessages + "'");
4450
this.error = error;
4551
this.details = details;
52+
this.sentMessages = sentMessages;
4653
}
4754

4855
public String getError() {
@@ -52,4 +59,8 @@ public String getError() {
5259
public List<ErrorDetail> getDetails() {
5360
return Collections.unmodifiableList(details);
5461
}
62+
63+
public List<SentMessage> getSentMessages() {
64+
return Collections.unmodifiableList(sentMessages);
65+
}
5566
}

clients/line-bot-messaging-api-client/src/main/java/com/linecorp/bot/messaging/client/MessagingApiExceptionBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ public MessagingApiExceptionBuilder() {
3232
protected IOException buildException(Response response, ErrorResponse errorBody) {
3333
return new MessagingApiClientException(
3434
response,
35-
errorBody.message(), errorBody.details());
35+
errorBody.message(), errorBody.details(), errorBody.sentMessages());
3636
}
3737
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2023 LINE Corporation
3+
*
4+
* LINE Corporation licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package com.linecorp.bot.messaging.client;
18+
19+
import okhttp3.Headers;
20+
import okhttp3.Request;
21+
import okhttp3.Response;
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Test;
24+
25+
import java.util.Collections;
26+
import java.util.List;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.mockito.Mockito.mock;
30+
import static org.mockito.Mockito.when;
31+
32+
import com.linecorp.bot.messaging.model.ErrorDetail;
33+
import com.linecorp.bot.messaging.model.SentMessage;
34+
35+
public class MessagingApiClientExceptionTest {
36+
37+
private Response response;
38+
39+
@BeforeEach
40+
void setUp() {
41+
response = mock(Response.class);
42+
Request request = new Request.Builder().url("https://example.com/").build();
43+
44+
when(response.request()).thenReturn(request);
45+
when(response.code()).thenReturn(200);
46+
47+
Headers headers = Headers.of(
48+
"x-line-example", "headerValue",
49+
"x-line-request-id", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
50+
);
51+
when(response.headers()).thenReturn(headers);
52+
}
53+
54+
@Test
55+
void constructAndGetTest() {
56+
List<ErrorDetail> errorDetails = Collections.singletonList(new ErrorDetail("message", "property"));
57+
List<SentMessage> sentMessages = Collections.singletonList(new SentMessage("IDIDID", "QUOQUOQUO"));
58+
String error = "testError";
59+
60+
MessagingApiClientException exception =
61+
new MessagingApiClientException(response, error, errorDetails, sentMessages);
62+
63+
assertThat(exception).isNotNull();
64+
assertThat(exception.getMessage()).isEqualTo(
65+
"API returns error: code=" + response.code() +
66+
" requestUrl=" + response.request().url() +
67+
" x-line-example=headerValue x-line-request-id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx " +
68+
"error='testError' " +
69+
"details='[ErrorDetail[message=message, property=property]]'" +
70+
" sentMessages='[SentMessage[id=IDIDID, quoteToken=QUOQUOQUO]]'");
71+
assertThat(exception.getError()).isEqualTo(error);
72+
assertThat(exception.getDetails()).isEqualTo(errorDetails);
73+
assertThat(exception.getSentMessages()).isEqualTo(sentMessages);
74+
}
75+
}

0 commit comments

Comments
 (0)