Search in sources :

Example 6 with ErrorResponse

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));
}
Also used : AvaticaSeverity(org.apache.calcite.avatica.AvaticaSeverity) RpcMetadataResponse(org.apache.calcite.avatica.remote.Service.RpcMetadataResponse) ErrorResponse(org.apache.calcite.avatica.remote.Service.ErrorResponse) Test(org.junit.Test)

Example 7 with ErrorResponse

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);
}
Also used : AvaticaSeverity(org.apache.calcite.avatica.AvaticaSeverity) NoSuchConnectionException(org.apache.calcite.avatica.NoSuchConnectionException) ErrorResponse(org.apache.calcite.avatica.remote.Service.ErrorResponse)

Example 8 with ErrorResponse

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);
}
Also used : HandlerResponse(org.apache.calcite.avatica.remote.Handler.HandlerResponse) ErrorResponse(org.apache.calcite.avatica.remote.Service.ErrorResponse) Response(org.apache.calcite.avatica.remote.Service.Response) IOException(java.io.IOException) ErrorResponse(org.apache.calcite.avatica.remote.Service.ErrorResponse) Test(org.junit.Test)

Example 9 with ErrorResponse

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());
    }
}
Also used : HandlerResponse(org.apache.calcite.avatica.remote.Handler.HandlerResponse) ErrorResponse(org.apache.calcite.avatica.remote.Service.ErrorResponse) Response(org.apache.calcite.avatica.remote.Service.Response) Request(org.apache.calcite.avatica.remote.Service.Request) IOException(java.io.IOException) ErrorResponse(org.apache.calcite.avatica.remote.Service.ErrorResponse) Test(org.junit.Test)

Example 10 with ErrorResponse

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());
}
Also used : IOException(java.io.IOException) ErrorResponse(org.apache.calcite.avatica.remote.Service.ErrorResponse) Test(org.junit.Test)

Aggregations

ErrorResponse (org.apache.calcite.avatica.remote.Service.ErrorResponse)10 Test (org.junit.Test)8 IOException (java.io.IOException)6 Response (org.apache.calcite.avatica.remote.Service.Response)6 HandlerResponse (org.apache.calcite.avatica.remote.Handler.HandlerResponse)4 AvaticaSeverity (org.apache.calcite.avatica.AvaticaSeverity)3 RpcMetadataResponse (org.apache.calcite.avatica.remote.Service.RpcMetadataResponse)3 Request (org.apache.calcite.avatica.remote.Service.Request)2 DataOutputStream (java.io.DataOutputStream)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 AvaticaClientRuntimeException (org.apache.calcite.avatica.AvaticaClientRuntimeException)1 AvaticaParameter (org.apache.calcite.avatica.AvaticaParameter)1