AbstractHandler implements Handler<T> { ErrorResponse unwrapException(Exception e) { int errorCode = ErrorResponse.UNKNOWN_ERROR_CODE; String sqlState = ErrorResponse.UNKNOWN_SQL_STATE; AvaticaSeverity severity = AvaticaSeverity.UNKNOWN; String errorMsg = null; if (e instanceof AvaticaRuntimeException) { AvaticaRuntimeException rte = (AvaticaRuntimeException) e; errorCode = rte.getErrorCode(); sqlState = rte.getSqlState(); severity = rte.getSeverity(); errorMsg = rte.getErrorMessage(); } else if (e instanceof NoSuchConnectionException) { errorCode = ErrorResponse.MISSING_CONNECTION_ERROR_CODE; severity = AvaticaSeverity.ERROR; errorMsg = e.getMessage(); } else { errorMsg = getCausalChain(e); } return new ErrorResponse(e, errorMsg, errorCode, sqlState, severity); } AbstractHandler(Service service); HandlerResponse<T> apply(T serializedRequest);  }
@Test public void testExceptionUnwrappingWithoutContext() { @SuppressWarnings("unchecked") AbstractHandler<String> handler = Mockito.mock(AbstractHandler.class); Mockito.when(handler.unwrapException(Mockito.any(Exception.class))).thenCallRealMethod(); Exception e = new RuntimeException(); Response resp = handler.unwrapException(e); assertTrue("Response should be ErrorResponse, but was " + resp.getClass(), resp instanceof ErrorResponse); ErrorResponse errorResp = (ErrorResponse) resp; assertEquals(ErrorResponse.UNKNOWN_ERROR_CODE, errorResp.errorCode); assertEquals(AvaticaSeverity.UNKNOWN, errorResp.severity); assertEquals(Arrays.asList(exceptionToString(e)), errorResp.exceptions); e = new AvaticaRuntimeException(); resp = handler.unwrapException(e); assertTrue("Response should be ErrorResponse, but was " + resp.getClass(), resp instanceof ErrorResponse); errorResp = (ErrorResponse) resp; assertEquals(ErrorResponse.UNKNOWN_ERROR_CODE, errorResp.errorCode); assertEquals(AvaticaSeverity.UNKNOWN, errorResp.severity); assertEquals(Arrays.asList(exceptionToString(e)), errorResp.exceptions); }
@Test public void testExceptionUnwrappingWithContext() { @SuppressWarnings("unchecked") AbstractHandler<String> handler = Mockito.mock(AbstractHandler.class); Mockito.when(handler.unwrapException(Mockito.any(Exception.class))).thenCallRealMethod(); final String msg = "Something failed!"; AvaticaRuntimeException e = new AvaticaRuntimeException(msg, ErrorResponse.UNKNOWN_ERROR_CODE, ErrorResponse.UNKNOWN_SQL_STATE, AvaticaSeverity.FATAL); Response resp = handler.unwrapException(e); assertTrue("Response should be ErrorResponse, but was " + resp.getClass(), resp instanceof ErrorResponse); ErrorResponse errorResp = (ErrorResponse) resp; assertEquals(ErrorResponse.UNKNOWN_ERROR_CODE, errorResp.errorCode); assertEquals(AvaticaSeverity.FATAL, errorResp.severity); assertEquals(Arrays.asList(exceptionToString(e)), errorResp.exceptions); assertEquals(msg, errorResp.errorMessage); }