private static EntityServiceException unwrap(final Throwable rootEx, final Throwable thEx, final int depth) {
        if (thEx instanceof EntityServiceException) {
            return (EntityServiceException) thEx;
        }
        if (thEx instanceof UnknownHostException) {
            return new EntityServiceException("Unknown host: " + thEx.getMessage(), thEx.getClass().getName(), true);
        }
        if (depth > 0) {
            if (thEx instanceof InvocationTargetException) {
                return unwrap(rootEx, ((InvocationTargetException) thEx).getTargetException(), depth - 1);
            }
            if (thEx.getCause() != null) {
                return unwrap(rootEx, thEx.getCause(), depth - 1);
            }
        }
        boolean networkRelated = thEx.getClass().getName().contains(".net");
        final String message = thEx.getMessage();
        if (message != null) {
            if (thEx instanceof IOException && message.contains("http")) {
                networkRelated = true;
            }
        }

        LOGGER.warn("unwrap() - wrapping exception {} {} (increase log level to debug to see stack trace)",
                rootEx.getMessage(), thEx.getMessage());
        LOGGER.debug("unwrap() - wrapping exception ", rootEx);

        final EntityServiceException entityServiceException = new EntityServiceException(unwrapMessage(rootEx, rootEx),
                thEx.getClass().getName(), networkRelated);
        entityServiceException.setCallStack(StreamUtil.exceptionCallStack(rootEx));
        return entityServiceException;

    }
Unwrap an exception and log it if required.
stroom-util/src/main/java/stroom/util/EntityServiceExceptionUtil.java