@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 = ((ByteBuf) httpResponse.getContent()).toString(ISO_8859_1); assertThat(content).isEqualTo("{\"message\":\"A message\"}"); assertThat(httpResponse.getStatus()).isEqualTo(HttpResponseStatus.PRECONDITION_FAILED); }
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 { 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 { 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); }
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); }
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);  }