public static StreamedRow error(final Throwable exception) { return new StreamedRow( null, new KsqlErrorMessage(Errors.ERROR_CODE_SERVER_ERROR, exception), null); }
public static <R> RestResponse<R> erroneous(final int errorCode, final String message) { return new Erroneous<>( new KsqlErrorMessage(errorCode, message)); }
public static Response commandQueueCatchUpTimeout(final String msg) { return Response .status(SERVICE_UNAVAILABLE) .entity(new KsqlErrorMessage(ERROR_CODE_COMMAND_QUEUE_CATCHUP_TIMEOUT, msg)) .build(); } }
public static Response badRequest(final String msg) { return Response .status(BAD_REQUEST) .entity(new KsqlErrorMessage(ERROR_CODE_BAD_REQUEST, msg)) .build(); }
public static Response badRequest(final Throwable t) { return Response .status(BAD_REQUEST) .entity(new KsqlErrorMessage(ERROR_CODE_BAD_REQUEST, t)) .build(); }
public static Response notFound(final String msg) { return Response .status(NOT_FOUND) .entity(new KsqlErrorMessage(ERROR_CODE_NOT_FOUND, msg)) .build(); }
@Override public Response toResponse(final Throwable exception) { // TODO: Distinguish between exceptions that warrant a stack trace and ones that don't if (exception instanceof KsqlRestException) { final KsqlRestException restException = (KsqlRestException)exception; return restException.getResponse(); } if (exception instanceof WebApplicationException) { final WebApplicationException webApplicationException = (WebApplicationException)exception; return Response .status( Response.Status.fromStatusCode( webApplicationException.getResponse().getStatus())) .type(MediaType.APPLICATION_JSON_TYPE) .entity( new KsqlErrorMessage( Errors.toErrorCode(webApplicationException.getResponse().getStatus()), webApplicationException)) .build(); } return Response .status(Response.Status.INTERNAL_SERVER_ERROR) .type(MediaType.APPLICATION_JSON_TYPE) .entity(new KsqlErrorMessage(Errors.ERROR_CODE_SERVER_ERROR, exception)) .build(); } }
@Test public void shouldHandleErrorMessageOnGetRequests() { // Given: givenServerWillReturn(new KsqlErrorMessage(12300, "ouch", ImmutableList.of("s1", "s2"))); // When: final RestResponse<?> response = ksqlRestClient.getServerInfo(); // Then: assertThat(response.getErrorMessage().getErrorCode(), is(12300)); assertThat(response.getErrorMessage().getMessage(), is("ouch")); assertThat(response.getErrorMessage().getStackTrace(), is(ImmutableList.of("s1", "s2"))); }
@Test public void shouldHandleErrorMessageOnPostRequests() { // Given: givenServerWillReturn(new KsqlErrorMessage(12300, "ouch", ImmutableList.of("s1", "s2"))); // When: final RestResponse<?> response = ksqlRestClient.makeKsqlRequest("whateva"); // Then: assertThat(response.getErrorMessage().getErrorCode(), is(12300)); assertThat(response.getErrorMessage().getMessage(), is("ouch")); assertThat(response.getErrorMessage().getStackTrace(), is(ImmutableList.of("s1", "s2"))); }
@Test public void shouldPrintErrorOnUnsupportedAPI() throws Exception { givenRunInteractivelyWillExit(); final KsqlRestClient mockRestClient = EasyMock.mock(KsqlRestClient.class); EasyMock.expect(mockRestClient.makeRootRequest()).andReturn( RestResponse.erroneous( new KsqlErrorMessage( Errors.toErrorCode(NOT_ACCEPTABLE.getStatusCode()), "Minimum supported client version: 1.0"))); EasyMock.expect(mockRestClient.getServerInfo()).andReturn( RestResponse.of(new ServerInfo("1.x", "testClusterId", "testServiceId"))); EasyMock.expect(mockRestClient.getServerAddress()).andReturn(new URI("http://someserver:8008")); EasyMock.replay(mockRestClient); new Cli(1L, 1L, mockRestClient, console) .runInteractively(); Assert.assertThat( terminal.getOutputString(), containsString("This CLI version no longer supported")); Assert.assertThat( terminal.getOutputString(), containsString("Minimum supported client version: 1.0")); }