@Override public HandlerResponse<byte[]> apply(byte[] requestBytes) { return super.apply(requestBytes); }
/** * Compute a response for the given request, handling errors generated by that computation. * * @param serializedRequest The caller's request. * @return A {@link Response} with additional context about that response. */ public HandlerResponse<T> apply(T serializedRequest) { try { final Service.Request request = decode(serializedRequest); final Service.Response response = request.accept(service); return new HandlerResponse<>(encode(response), HTTP_OK); } catch (Exception e) { return convertToErrorResponse(e); } }
private HandlerResponse<T> createErrorResponse(Exception e, int statusCode) { ErrorResponse errorResp = unwrapException(e); try { return new HandlerResponse<>(encode(errorResp), statusCode); } catch (IOException e1) { // TODO provide a canned ErrorResponse // If we can't serialize the error message, we can't give a meaningful error to caller. // Just try to not unnecessarily create more exceptions. if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new RuntimeException(e); } }
@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()); } }
final Service.Request request; try { request = decode(serializedRequest); } catch (IOException e) { return new HandlerResponse<>(encode(response), HTTP_OK); } catch (Exception e) { ErrorResponse errorResp = unwrapException(e); return new HandlerResponse<>(encode(errorResp), HTTP_INTERNAL_SERVER_ERROR); } catch (IOException e1) {
@Test public void testFailedRequestDeserialization() throws IOException { @SuppressWarnings("unchecked") final AbstractHandler<String> handler = Mockito.mock(AbstractHandler.class); final IOException exception = new IOException(); // 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); try { handler.apply("this is mocked out"); } catch (RuntimeException e) { assertEquals(exception, e.getCause()); } } }
@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); }
/** * 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); }
/** * Attempts to convert an Exception to an ErrorResponse with an HTTP stauts code of {@code 403}. */ public HandlerResponse<T> unauthorizedErrorResponse(Exception e) { return createErrorResponse(e, HTTP_UNAUTHORIZED); }
@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()); } }
@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); }
/** * 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); }
/** * Attempts to convert an Exception to an ErrorResponse. If there is an issue in serialization, * a RuntimeException is thrown instead (wrapping the original exception if necessary). * * @param e The exception to convert. * @return A HandlerResponse instance. */ public HandlerResponse<T> convertToErrorResponse(Exception e) { return createErrorResponse(e, HTTP_INTERNAL_SERVER_ERROR); }
@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(); final String serializedErrorResponse = "Serialized ErrorResponse"; // Faked out // 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()); } }
/** * Compute a response for the given request, handling errors generated by that computation. * * @param serializedRequest The caller's request. * @return A {@link Response} with additional context about that response. */ public HandlerResponse<T> apply(T serializedRequest) { try { final Service.Request request = decode(serializedRequest); final Service.Response response = request.accept(service); return new HandlerResponse<>(encode(response), HTTP_OK); } catch (Exception e) { return convertToErrorResponse(e); } }
private HandlerResponse<T> createErrorResponse(Exception e, int statusCode) { ErrorResponse errorResp = unwrapException(e); try { return new HandlerResponse<>(encode(errorResp), statusCode); } catch (IOException e1) { // TODO provide a canned ErrorResponse // If we can't serialize the error message, we can't give a meaningful error to caller. // Just try to not unnecessarily create more exceptions. if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new RuntimeException(e); } }
public HandlerResponse<String> apply(String jsonRequest) { return super.apply(jsonRequest); }
@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); }
/** * 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); }
/** * Attempts to convert an Exception to an ErrorResponse with an HTTP status code of {@code 401}. */ public HandlerResponse<T> unauthenticatedErrorResponse(Exception e) { return createErrorResponse(e, HTTP_UNAUTHENTICATED); }