use of org.apache.calcite.avatica.remote.Service.ErrorResponse in project calcite-avatica by apache.
the class ErrorResponseTest method testEquality.
@Test
public void testEquality() {
final String message = "There was an error";
final int code = 23;
final String state = "a1b2c";
final AvaticaSeverity severity = AvaticaSeverity.ERROR;
final List<String> exceptions = Arrays.asList("Server Stacktrace 1", "Server Stacktace 2");
final RpcMetadataResponse metadata = new RpcMetadataResponse("localhost:8765");
assertEquals(new ErrorResponse(message, code, state, severity, exceptions, metadata), new ErrorResponse(message, code, state, severity, exceptions, metadata));
}
use of org.apache.calcite.avatica.remote.Service.ErrorResponse in project calcite-avatica by apache.
the class AbstractHandler method unwrapException.
/**
* Unwrap Avatica-specific context about a given exception.
*
* @param e A caught exception throw by Avatica implementation.
* @return An {@link ErrorResponse}.
*/
ErrorResponse unwrapException(Exception e) {
// By default, we know nothing extra.
int errorCode = ErrorResponse.UNKNOWN_ERROR_CODE;
String sqlState = ErrorResponse.UNKNOWN_SQL_STATE;
AvaticaSeverity severity = AvaticaSeverity.UNKNOWN;
String errorMsg = null;
// Extract the contextual information if we have it. We may not.
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 {
// Try to construct a meaningful error message when the server impl doesn't provide one.
errorMsg = getCausalChain(e);
}
return new ErrorResponse(e, errorMsg, errorCode, sqlState, severity, metadata);
}
use of org.apache.calcite.avatica.remote.Service.ErrorResponse in project calcite-avatica by apache.
the class AbstractHandlerTest method testExceptionUnwrappingWithoutContext.
@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);
}
use of org.apache.calcite.avatica.remote.Service.ErrorResponse in project calcite-avatica by apache.
the class AbstractHandlerTest method testFailedErrorResponseSerialization.
@Test
public void testFailedErrorResponseSerialization() throws IOException {
@SuppressWarnings("unchecked") final AbstractHandler<String> handler = Mockito.mock(AbstractHandler.class);
final Request request = Mockito.mock(Request.class);
final Response response = Mockito.mock(Response.class);
final IOException exception = new IOException();
final ErrorResponse errorResponse = Mockito.mock(ErrorResponse.class);
// Accept a serialized request
Mockito.when(handler.apply(Mockito.anyString())).thenCallRealMethod();
// Deserialize it back into a POJO
Mockito.when(handler.decode(Mockito.anyString())).thenReturn(request);
// Construct the Response for that Request
Mockito.when(request.accept(Mockito.any(Service.class))).thenReturn(response);
// Throw an IOException when serializing the Response.
Mockito.when(handler.encode(response)).thenThrow(exception);
// Convert the IOException into an ErrorResponse
Mockito.when(handler.unwrapException(exception)).thenReturn(errorResponse);
// Fail to serialize the ErrorResponse
Mockito.when(handler.encode(errorResponse)).thenThrow(exception);
try {
handler.apply("this is mocked out");
} catch (RuntimeException e) {
assertEquals(exception, e.getCause());
}
}
use of org.apache.calcite.avatica.remote.Service.ErrorResponse in project calcite-avatica by apache.
the class AbstractHandlerTest method testFailedRequestDeserialization.
@Test
public void testFailedRequestDeserialization() throws IOException {
@SuppressWarnings("unchecked") final AbstractHandler<String> handler = Mockito.mock(AbstractHandler.class);
final IOException exception = new IOException();
final ErrorResponse errorResponse = new ErrorResponse();
// Faked out
final String serializedErrorResponse = "Serialized ErrorResponse";
// Accept a serialized request
Mockito.when(handler.apply(Mockito.anyString())).thenCallRealMethod();
// Throw an Exception trying to convert it back into a POJO
Mockito.when(handler.decode(Mockito.anyString())).thenThrow(exception);
Mockito.when(handler.convertToErrorResponse(exception)).thenCallRealMethod();
Mockito.when(handler.unwrapException(exception)).thenReturn(errorResponse);
Mockito.when(handler.encode(errorResponse)).thenReturn(serializedErrorResponse);
HandlerResponse<String> response = handler.apply("this is mocked out");
assertEquals(serializedErrorResponse, response.getResponse());
assertEquals(500, response.getStatusCode());
}
Aggregations