CommonHandler { CommonResponse newHttpResponseFromException(CommonRequest request, Authentication authentication, Exception exception) throws Exception { Exception e = exception; if (e instanceof InvocationTargetException) { Throwable cause = e.getCause(); if (cause instanceof Exception) { e = (Exception) cause; } } if (e instanceof JsonServiceException) { JsonServiceException jsonServiceException = (JsonServiceException) e; if (jsonServiceException.getStatus() == FORBIDDEN) { return handleNotAuthorized(request, authentication); } else { return newHttpResponseWithMessage(jsonServiceException.getStatus(), jsonServiceException.getMessage()); } } logger.error(e.getMessage(), e); if (e instanceof SQLException && ((SQLException) e).getErrorCode() == H2_STATEMENT_WAS_CANCELED) { return newHttpResponseWithMessage(REQUEST_TIMEOUT, "Query timed out (timeout is configurable under Configuration > Advanced)"); } return newHttpResponseWithStackTrace(e, INTERNAL_SERVER_ERROR, null); } CommonHandler(boolean central, LayoutService layoutService,
            Map<Pattern, HttpService> httpServices, HttpSessionManager httpSessionManager,
            List<Object> jsonServices, Clock clock); CommonResponse handle(CommonRequest request);  }
@Test public void shouldCreateJsonServiceExceptionResponse() throws Exception { Exception e = new JsonServiceException(HttpResponseStatus.PRECONDITION_FAILED, new IllegalStateException("An ignored message")); CommonResponse httpResponse = HTTP_SERVER_HANDLER.newHttpResponseFromException( mock(CommonRequest.class), mock(Authentication.class), e); String content = (String) httpResponse.getContent(); assertThat(content).isEqualTo("{\"message\":\"\"}"); assertThat(httpResponse.getStatus()).isEqualTo(HttpResponseStatus.PRECONDITION_FAILED); }
@Test public void shouldCreateJsonServiceExceptionResponseWithMessage() throws Exception { Exception e = new JsonServiceException(HttpResponseStatus.PRECONDITION_FAILED, "A message"); CommonResponse httpResponse = HTTP_SERVER_HANDLER.newHttpResponseFromException( mock(CommonRequest.class), mock(Authentication.class), e); String content = (String) httpResponse.getContent(); assertThat(content).isEqualTo("{\"message\":\"A message\"}"); assertThat(httpResponse.getStatus()).isEqualTo(HttpResponseStatus.PRECONDITION_FAILED); }
@Test public void shouldCreateJsonServiceExceptionResponseFromInvocationTargetException() throws Exception { Exception e = new InvocationTargetException( new JsonServiceException(HttpResponseStatus.PRECONDITION_FAILED, new IllegalStateException("An ignored message"))); CommonResponse httpResponse = HTTP_SERVER_HANDLER.newHttpResponseFromException( mock(CommonRequest.class), mock(Authentication.class), e); String content = (String) httpResponse.getContent(); assertThat(content).isEqualTo("{\"message\":\"\"}"); assertThat(httpResponse.getStatus()).isEqualTo(HttpResponseStatus.PRECONDITION_FAILED); }
@Test public void shouldCreateJsonServiceExceptionResponseWithMessageFromInvocationTargetException() throws Exception { Exception e = new InvocationTargetException( new JsonServiceException(HttpResponseStatus.PRECONDITION_FAILED, "A message")); CommonResponse httpResponse = HTTP_SERVER_HANDLER.newHttpResponseFromException( mock(CommonRequest.class), mock(Authentication.class), e); String content = (String) httpResponse.getContent(); assertThat(content).isEqualTo("{\"message\":\"A message\"}"); assertThat(httpResponse.getStatus()).isEqualTo(HttpResponseStatus.PRECONDITION_FAILED); }
@Test public void shouldCreateSqlTimeoutResponse() throws Exception { Exception e = new SQLException("", "", H2_STATEMENT_WAS_CANCELED); CommonResponse httpResponse = HTTP_SERVER_HANDLER.newHttpResponseFromException( mock(CommonRequest.class), mock(Authentication.class), e); String content = (String) httpResponse.getContent(); assertThat(content).isEqualTo("{\"message\":" + "\"Query timed out (timeout is configurable under Configuration > Advanced)\"}"); assertThat(httpResponse.getStatus()).isEqualTo(HttpResponseStatus.REQUEST_TIMEOUT); }
@Test public void shouldCreateNonTimeoutSqlExceptionResponse() throws Exception { Exception e = new SQLException("Another message", "", H2_STATEMENT_WAS_CANCELED + 1); CommonResponse httpResponse = HTTP_SERVER_HANDLER.newHttpResponseFromException( mock(CommonRequest.class), mock(Authentication.class), e); String content = (String) httpResponse.getContent(); ObjectNode node = (ObjectNode) new ObjectMapper().readTree(content); assertThat(node.get("message").asText()) .isEqualTo("java.sql.SQLException: Another message"); assertThat(node.get("stackTrace")).isNotNull(); assertThat(httpResponse.getStatus()).isEqualTo(HttpResponseStatus.INTERNAL_SERVER_ERROR); }
@Test public void shouldCreateExceptionResponse() throws Exception { Exception e = new Exception("Banother message"); CommonResponse httpResponse = HTTP_SERVER_HANDLER.newHttpResponseFromException( mock(CommonRequest.class), mock(Authentication.class), e); String content = (String) httpResponse.getContent(); ObjectNode node = (ObjectNode) new ObjectMapper().readTree(content); assertThat(node.get("message").asText()).isEqualTo("java.lang.Exception: Banother message"); assertThat(node.get("stackTrace")).isNotNull(); assertThat(httpResponse.getStatus()).isEqualTo(HttpResponseStatus.INTERNAL_SERVER_ERROR); }
@Test public void shouldCreateWrappedExceptionResponse() throws Exception { Exception e = new Exception(new Exception(new Exception("Wrapped message"))); CommonResponse httpResponse = HTTP_SERVER_HANDLER.newHttpResponseFromException( mock(CommonRequest.class), mock(Authentication.class), e); String content = (String) httpResponse.getContent(); ObjectNode node = (ObjectNode) new ObjectMapper().readTree(content); assertThat(node.get("message").asText()).isEqualTo("java.lang.Exception: Wrapped message"); assertThat(node.get("stackTrace")).isNotNull(); assertThat(httpResponse.getStatus()).isEqualTo(HttpResponseStatus.INTERNAL_SERVER_ERROR); }