ExceptionConverter implements Function<Response<?>, LineMessagingException> { @Override public LineMessagingException apply(Response<?> response) { final String requestId = response.headers().get("x-line-request-id"); try { return applyInternal(requestId, response); } catch (Exception e) { final ErrorResponse errorResponse = new ErrorResponse(requestId, null, null); return new GeneralLineMessagingException(e.getMessage(), errorResponse, e); } }  @Override LineMessagingException apply(Response<?> response); static final ObjectReader OBJECT_READER; }
@Test public void exceptionInConvertFallbackedTest() { final ResponseBody responseBody = mock(ResponseBody.class); when(responseBody.source()).thenThrow(new RuntimeException()); final LineMessagingException result = target.apply(Response.error(401, responseBody)); assertThat(result) .isInstanceOf(GeneralLineMessagingException.class); }
@Test public void requestIdDeserializationTest() { final ResponseBody responseBody = ResponseBody.create(MediaType.parse("application/json"), "{\"message\":\"Invalid reply token\"}"); final okhttp3.Response rawResponse = new Builder() .code(400) .message("") .request(new Request.Builder().get().url("https: .addHeader("X-Line-Request-Id", "5ac44e02-e6be-49c3-a55f-6b2a29bc3aa4") .protocol(Protocol.HTTP_1_1) .build(); assertThat(rawResponse.header("X-Line-Request-Id")).isEqualTo("5ac44e02-e6be-49c3-a55f-6b2a29bc3aa4"); final LineMessagingException result = target.apply(Response.error(responseBody, rawResponse)); assertThat(result.getErrorResponse().getRequestId()).isEqualTo("5ac44e02-e6be-49c3-a55f-6b2a29bc3aa4"); }
@Test public void convertTest() { final ResponseBody responseBody = ResponseBody.create(MediaType.parse("application/json"), "{}"); final LineMessagingException result = target.apply(Response.error(401, responseBody)); assertThat(result) .isInstanceOf(UnauthorizedException.class); }
@Test public void convertUnknownExceptionTest() { final ResponseBody responseBody = ResponseBody.create(MediaType.parse("application/json"), "{}"); final LineMessagingException result = target.apply(Response.error(999, responseBody)); assertThat(result) .isInstanceOf(GeneralLineMessagingException.class); }