ErrorMessageFactory { public static ErrorMessage createErrorMessage(Exception e, int status) { Throwable cause = unwrapCause(e); if (cause instanceof ElasticsearchException) { ElasticsearchException exception = (ElasticsearchException) cause; return new ElasticsearchErrorMessage(exception, exception.status().getStatus()); } return new ErrorMessage(e, status); }  static ErrorMessage createErrorMessage(Exception e, int status);  }
@Test public void esExceptionShouldCreateEsErrorMessage() { Exception exception = new ElasticsearchException(nonEsThrowable); ErrorMessage msg = ErrorMessageFactory.createErrorMessage(exception, RestStatus.BAD_REQUEST.getStatus()); assertTrue(msg instanceof ElasticsearchErrorMessage); }
@Test public void nonEsExceptionShouldCreateGenericErrorMessage() { Exception exception = new Exception(nonEsThrowable); ErrorMessage msg = ErrorMessageFactory.createErrorMessage(exception, RestStatus.BAD_REQUEST.getStatus()); assertFalse(msg instanceof ElasticsearchErrorMessage); }
@Test public void nonEsExceptionWithWrappedEsExceptionCauseShouldCreateEsErrorMessage() { Exception exception = (Exception) esThrowable; ErrorMessage msg = ErrorMessageFactory.createErrorMessage(exception, RestStatus.BAD_REQUEST.getStatus()); assertTrue(msg instanceof ElasticsearchErrorMessage); }
@Test public void nonEsExceptionWithMultiLayerWrappedEsExceptionCauseShouldCreateEsErrorMessage() { Exception exception = new Exception(new Throwable(new Throwable(esThrowable))); ErrorMessage msg = ErrorMessageFactory.createErrorMessage(exception, RestStatus.BAD_REQUEST.getStatus()); assertTrue(msg instanceof ElasticsearchErrorMessage); }