private String formatError(Throwable ex) {
        final StringBuffer buffer = new StringBuffer();
        for ( Throwable e = ex; e != null; e = ex.getCause() ) {
            buffer.append(ex.getMessage()).append("\n");
            StackTraceElement[] stackTrace = ex.getStackTrace();
            if ( null == stackTrace ) {
                continue;
            }
            for ( StackTraceElement stackTraceElement : stackTrace ) {
                buffer.append(stackTraceElement.toString()).append("\n");
            }
        }
        return buffer.toString();
    }