Skip to content

Commit

Permalink
support sentMessages in MessagingApiClientException (#1159)
Browse files Browse the repository at this point in the history
  • Loading branch information
tokuhirom committed Nov 17, 2023
1 parent fc1486f commit 934020c
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

public MessagingApiClientException(Response response, String error, List<ErrorDetail> details) {
super(response, "error='" + error + "' details='" + details + "'");
/**
* Array of sent messages.
*/
private final List<SentMessage> sentMessages;

public MessagingApiClientException(Response response, String error, List<ErrorDetail> details, List<SentMessage> sentMessages) {
super(response, "error='" + error + "' details='" + details + "' sentMessages='" + sentMessages + "'");
this.error = error;
this.details = details;
this.sentMessages = sentMessages;
}

public String getError() {
Expand All @@ -52,4 +59,8 @@ public String getError() {
public List<ErrorDetail> getDetails() {
return Collections.unmodifiableList(details);
}

public List<SentMessage> getSentMessages() {
return Collections.unmodifiableList(sentMessages);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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<ErrorDetail> errorDetails = Collections.singletonList(new ErrorDetail("message", "property"));
List<SentMessage> 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);
}
}

0 comments on commit 934020c

Please sign in to comment.