Example usage for java.lang Throwable getCause

List of usage examples for java.lang Throwable getCause

Introduction

In this page you can find the example usage for java.lang Throwable getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:com.adito.boot.Util.java

/**
 * Concatenate all of the non null messages from an exception chain,
 * appending full stops after each messages if they do not end with one.
 * /*  w w w  .jav a2  s. co m*/
 * @param t trace
 * @return exception message chain text
 */
public static String getExceptionMessageChain(Throwable t) {
    StringBuffer buf = new StringBuffer();
    while (t != null) {
        if (buf.length() > 0 && !buf.toString().endsWith(".")) {
            buf.append(". ");
        }
        if (t.getMessage() != null) {
            buf.append(t.getMessage().trim());
        }
        t = t.getCause();
    }
    return buf.toString();
}

From source file:fr.certu.chouette.command.ExchangeCommand.java

/**
 * @param beans//from   ww  w  .  ja  v  a 2s .  c om
 * @param manager
 * @param parameters
 */
public void executeExportDeletion(List<NeptuneIdentifiedObject> beans,
        INeptuneManager<NeptuneIdentifiedObject> manager, Map<String, List<String>> parameters) {
    String format = getSimpleString(parameters, "format");
    try {
        List<FormatDescription> formats = manager.getDeleteExportFormats(null);
        FormatDescription description = null;

        for (FormatDescription formatDescription : formats) {
            if (formatDescription.getName().equalsIgnoreCase(format)) {
                description = formatDescription;
                break;
            }
        }
        if (description == null) {
            throw new IllegalArgumentException(
                    "format " + format + " unavailable, check command getDeletionExportFormats for list ");
        }

        List<ParameterValue> values = new ArrayList<ParameterValue>();
        for (ParameterDescription desc : description.getParameterDescriptions()) {
            String name = desc.getName();
            String key = name.toLowerCase();
            List<String> vals = parameters.get(key);
            if (vals == null) {
                if (desc.isMandatory()) {
                    throw new IllegalArgumentException("parameter -" + name
                            + " is required, check command getDeletionExportFormats for list ");
                }
            } else {
                if (desc.isCollection()) {
                    ListParameterValue val = new ListParameterValue(name);
                    switch (desc.getType()) {
                    case FILEPATH:
                        val.setFilepathList(vals);
                        break;
                    case STRING:
                        val.setStringList(vals);
                        break;
                    case FILENAME:
                        val.setFilenameList(vals);
                        break;
                    default:
                        throw new IllegalArgumentException("parameter -" + name
                                + " invalid, check command getDeletionExportFormats for list ");
                    }
                    values.add(val);
                } else {
                    if (vals.size() != 1) {
                        throw new IllegalArgumentException("parameter -" + name
                                + " must be unique, check command getDeletionExportFormats for list ");
                    }
                    String simpleval = vals.get(0);

                    SimpleParameterValue val = new SimpleParameterValue(name);
                    switch (desc.getType()) {
                    case FILEPATH:
                        val.setFilepathValue(simpleval);
                        break;
                    case STRING:
                        val.setStringValue(simpleval);
                        break;
                    case FILENAME:
                        val.setFilenameValue(simpleval);
                        break;
                    case BOOLEAN:
                        val.setBooleanValue(Boolean.parseBoolean(simpleval));
                        break;
                    case INTEGER:
                        val.setIntegerValue(Long.parseLong(simpleval));
                        break;
                    default:
                        throw new IllegalArgumentException("parameter -" + name
                                + " invalid, check command getDeletionExportFormats for list ");
                    }
                    values.add(val);
                }
            }
        }

        ReportHolder holder = new ReportHolder();
        manager.doExportDeleted(null, beans, format, values, holder);
        if (holder.getReport() != null) {
            Report r = holder.getReport();
            System.out.println(r.getLocalizedMessage());
            printItems(System.out, "", r.getItems());
        }
    } catch (ChouetteException e) {
        log.error(e.getMessage());

        Throwable caused = e.getCause();
        while (caused != null) {
            log.error("caused by " + caused.getMessage());
            caused = caused.getCause();
        }
        throw new RuntimeException("export failed, see details in log");
    }
}

From source file:fr.certu.chouette.command.ExchangeCommand.java

/**
 * @param beans// w  w w  .j  av  a2 s.  c o  m
 * @param manager
 * @param parameters
 */
@SuppressWarnings("incomplete-switch")
public void executeExport(List<NeptuneIdentifiedObject> beans, INeptuneManager<NeptuneIdentifiedObject> manager,
        Map<String, List<String>> parameters) {
    String format = getSimpleString(parameters, "format");
    try {
        List<FormatDescription> formats = manager.getExportFormats(null);
        FormatDescription description = null;

        for (FormatDescription formatDescription : formats) {
            if (formatDescription.getName().equalsIgnoreCase(format)) {
                description = formatDescription;
                break;
            }
        }
        if (description == null) {
            throw new IllegalArgumentException(
                    "format " + format + " unavailable, check command getExportFormats for list ");
        }

        List<ParameterValue> values = new ArrayList<ParameterValue>();
        for (ParameterDescription desc : description.getParameterDescriptions()) {
            String name = desc.getName();
            String key = name.toLowerCase();
            List<String> vals = parameters.get(key);
            if (vals == null) {
                if (desc.isMandatory()) {
                    throw new IllegalArgumentException(
                            "parameter -" + name + " is required, check command getExportFormats for list ");
                }
            } else {
                if (desc.isCollection()) {
                    ListParameterValue val = new ListParameterValue(name);
                    switch (desc.getType()) {
                    case FILEPATH:
                        val.setFilepathList(vals);
                        break;
                    case STRING:
                        val.setStringList(vals);
                        break;
                    case FILENAME:
                        val.setFilenameList(vals);
                        break;
                    default:
                        throw new IllegalArgumentException("parameter -" + name + " is invalid ");
                    }
                    values.add(val);
                } else {
                    if (vals.size() != 1) {
                        throw new IllegalArgumentException("parameter -" + name
                                + " must be unique, check command getExportFormats for list ");
                    }
                    String simpleval = vals.get(0);

                    SimpleParameterValue val = new SimpleParameterValue(name);
                    switch (desc.getType()) {
                    case FILEPATH:
                        val.setFilepathValue(simpleval);
                        break;
                    case STRING:
                        val.setStringValue(simpleval);
                        break;
                    case FILENAME:
                        val.setFilenameValue(simpleval);
                        break;
                    case BOOLEAN:
                        val.setBooleanValue(Boolean.parseBoolean(simpleval));
                        break;
                    case INTEGER:
                        val.setIntegerValue(Long.parseLong(simpleval));
                        break;
                    case DATE:
                        val.setDateValue(toCalendar(simpleval));
                        break;
                    }
                    values.add(val);
                }
            }
        }

        ReportHolder holder = new ReportHolder();
        manager.doExport(null, beans, format, values, holder);
        PrintStream stream = System.out;
        if (holder.getReport() != null) {
            Report r = holder.getReport();
            stream.println(r.getLocalizedMessage());
            printItems(stream, "", r.getItems());
        }
    } catch (ChouetteException e) {
        log.error(e.getMessage());

        Throwable caused = e.getCause();
        while (caused != null) {
            log.error("caused by " + caused.getMessage());
            caused = caused.getCause();
        }
        throw new RuntimeException("export failed, see details in log");
    }
}

From source file:com.acciente.induction.dispatcher.HttpDispatcher.java

private Throwable getRootCause(Throwable oError) {
    while (oError.getCause() != null && oError.getCause() != oError) {
        oError = oError.getCause();/* w ww  .  ja v  a 2 s.  co m*/
    }

    return oError;
}

From source file:jp.terasoluna.fw.util.ExceptionUtil.java

/**
 * ?????//from  w  ww.  ja  v  a 2 s . c o m
 * 
 * <p>
 *  ??????????????
 *  ????????
 *  ???getRootCause()????????ServletException??
 * </p>
 *
 * @param throwable 
 * @return ???
 */
public static String getStackTrace(Throwable throwable) {
    StringBuilder sb = new StringBuilder();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    while (throwable != null) {
        baos.reset();
        throwable.printStackTrace(new PrintStream(baos));
        sb.append(baos.toString());

        //throwable?Class??
        Class<? extends Throwable> throwableClass = throwable.getClass();

        // ServletException ?? getRootCause ?
        if (SERVLET_EXCEPTION_NAME.equals(throwableClass.getName())) {
            try {
                //throwable = ((ServletException) throwable).getRootCause()
                //Class??????
                Method method = throwableClass.getMethod(GET_ROOT_CAUSE);
                throwable = (Throwable) method.invoke(throwable);
            } catch (NoSuchMethodException e) {
                //???????
                log.error(e.getMessage());
                throwable = null;
            } catch (IllegalAccessException e) {
                //?????????
                log.error(e.getMessage());
                throwable = null;
            } catch (InvocationTargetException e) {
                //?????
                log.error(e.getMessage());
                throwable = null;
            }

        } else {
            throwable = throwable.getCause();
        }
    }
    return sb.toString();
}

From source file:eu.domibus.ebms3.receiver.FaultInHandler.java

@Override
/**/*  www . j  a va  2  s  .  c o m*/
 * The {@code handleFault} method is responsible for handling and conversion of exceptions
 * thrown during the processing of incoming ebMS3 messages
 */
public boolean handleFault(SOAPMessageContext context) {

    Exception exception = (Exception) context.get(Exception.class.getName());
    Throwable cause = exception.getCause();
    EbMS3Exception ebMS3Exception = null;
    if (cause != null) {

        if (!(cause instanceof EbMS3Exception)) {
            //do Mapping of non ebms exceptions
            if (cause instanceof NoMatchingPModeFoundException) {
                ebMS3Exception = new EbMS3Exception(EbMS3Exception.EbMS3ErrorCode.EBMS_0010, cause.getMessage(),
                        ((NoMatchingPModeFoundException) cause).getMessageId(), cause, MSHRole.RECEIVING);
            } else {

                if (cause instanceof WebServiceException) {
                    if (cause.getCause() instanceof EbMS3Exception) {
                        ebMS3Exception = (EbMS3Exception) cause.getCause();
                    }
                } else {
                    ebMS3Exception = new EbMS3Exception(EbMS3Exception.EbMS3ErrorCode.EBMS_0004, "", cause,
                            MSHRole.RECEIVING);
                }
            }

        } else {
            ebMS3Exception = (EbMS3Exception) cause;
        }

        this.processEbMSError(context, ebMS3Exception);

    } else {
        if (exception instanceof PolicyException) {
            //FIXME: use a consistent way of property exchange between JAXWS and CXF message model. This: PhaseInterceptorChain
            String messageId = (String) PhaseInterceptorChain.getCurrentMessage()
                    .getContextualProperty("ebms.messageid");

            ebMS3Exception = new EbMS3Exception(EbMS3Exception.EbMS3ErrorCode.EBMS_0103, exception.getMessage(),
                    messageId, exception, MSHRole.RECEIVING);
        } else {
            ebMS3Exception = new EbMS3Exception(EbMS3Exception.EbMS3ErrorCode.EBMS_0004, "", cause,
                    MSHRole.RECEIVING);
        }

        this.processEbMSError(context, ebMS3Exception);
    }

    return true;
}

From source file:io.druid.storage.s3.S3DataSegmentPuller.java

@Override
public Predicate<Throwable> shouldRetryPredicate() {
    // Yay! smart retries!
    return new Predicate<Throwable>() {
        @Override/*from  ww  w. jav  a2  s. co m*/
        public boolean apply(Throwable e) {
            if (e == null) {
                return false;
            }
            if (e instanceof ServiceException) {
                return S3Utils.isServiceExceptionRecoverable((ServiceException) e);
            }
            if (S3Utils.S3RETRY.apply(e)) {
                return true;
            }
            // Look all the way down the cause chain, just in case something wraps it deep.
            return apply(e.getCause());
        }
    };
}

From source file:net.dv8tion.jda.core.requests.RestAction.java

/**
 * Blocks the current Thread and awaits the completion
 * of an {@link #submit()} request./*from w  ww  . j a v a2  s  .  c om*/
 * <br>Used for synchronous logic.
 *
 * @param  shouldQueue
 *         Whether this should automatically handle rate limitations (default true)
 *
 * @throws RateLimitedException
 *         If we were rate limited and the {@code shouldQueue} is false
 *         <br>Use {@link #complete()} to avoid this Exception.
 *
 * @return The response value
 */
public T complete(boolean shouldQueue) throws RateLimitedException {
    try {
        return submit(shouldQueue).get();
    } catch (Throwable e) {
        if (e instanceof ExecutionException) {
            Throwable t = e.getCause();
            if (t instanceof RateLimitedException)
                throw (RateLimitedException) t;
            else if (t instanceof PermissionException)
                throw (PermissionException) t;
            else if (t instanceof ErrorResponseException)
                throw (ErrorResponseException) t;
        }
        throw new RuntimeException(e);
    }
}

From source file:edu.usu.sdl.openstorefront.common.util.StringProcessor.java

/**
 * This will produce html highlighted stacktrace
 *
 * @param throwable/*w w  w.  ja  va 2s. com*/
 * @return
 */
public static String parseStackTraceHtml(Throwable throwable) {
    StringBuilder exception = new StringBuilder();

    if (throwable != null) {
        String message = throwable.getMessage();
        if (StringUtils.isNotBlank(message)) {
            exception.append("<b>Message:</b> <span style='color: red;'><b>")
                    .append(message.replace("\n", "<br>")).append("</b><br>");
        }
        for (StackTraceElement stackTraceElement : throwable.getStackTrace()) {
            String style = "color: grey; font-size: 10px;";
            if (stackTraceElement.getClassName().contains("edu.usu.sdl")) {
                style = "color: black; font-size: 12px; font-wieght: bold;";
            }
            exception.append("<span style='").append(style).append("'>")
                    .append(stackTraceElement.getClassName()).append(" (")
                    .append(stackTraceElement.getMethodName()).append(") : ")
                    .append(stackTraceElement.getLineNumber()).append(" ").append("</span><br>");
        }
        if (throwable.getSuppressed().length > 0) {
            exception.append("Suppress Exceptions: ");
            for (Throwable suppressed : throwable.getSuppressed()) {
                exception.append(parseStackTraceHtml(suppressed)).append("<br>");
            }
        }

        if (throwable.getCause() != null) {

        }
    }

    return exception.toString();
}

From source file:com.icesoft.faces.webapp.xmlhttp.PersistentFacesState.java

private void throwRenderingException(Exception e) throws FatalRenderingException, TransientRenderingException {
    Throwable throwable = e;
    while (throwable != null) {
        if (throwable instanceof IllegalStateException || throwable instanceof SessionExpiredException) {
            fatalRenderingException(e);//ww  w . j  a  v a 2s. co  m
        } else {
            throwable = throwable.getCause();
        }
    }
    transientRenderingException(e);
}