Example usage for org.apache.commons.lang3.exception ExceptionUtils getRootCause

List of usage examples for org.apache.commons.lang3.exception ExceptionUtils getRootCause

Introduction

In this page you can find the example usage for org.apache.commons.lang3.exception ExceptionUtils getRootCause.

Prototype

public static Throwable getRootCause(final Throwable throwable) 

Source Link

Document

Introspects the Throwable to obtain the root cause.

This method walks through the exception chain to the last element, "root" of the tree, using #getCause(Throwable) , and returns that exception.

From version 2.2, this method handles recursive cause structures that might otherwise cause infinite loops.

Usage

From source file:org.xwiki.rendering.internal.util.DefaultErrorBlockGenerator.java

@Override
public List<Block> generateErrorBlocks(String messagePrefix, Throwable throwable, boolean isInline) {
    // Note: We're using ExceptionUtils.getRootCause(e).getMessage() instead of getRootCauseMessage()
    //       below because getRootCauseMessage() adds a technical prefix (the name of the exception), that
    //       we don't want to display to our users.
    Throwable rootCause = ExceptionUtils.getRootCause(throwable);
    if (rootCause == null) {
        // If there's no nested exception, fall back to the throwable itself for getting the cause
        rootCause = throwable;//from w w  w . jav a2 s  . c o m
    }

    String augmentedMessage = String.format("%s%s", messagePrefix,
            rootCause == null ? "" : String.format(". Cause: [%s]", rootCause.getMessage()));
    augmentedMessage = String.format("%s%sClick on this message for details.", augmentedMessage,
            augmentedMessage.trim().endsWith(".") ? " " : ". ");

    this.logger.debug(augmentedMessage);

    return generateErrorBlocks(augmentedMessage, ExceptionUtils.getStackTrace(throwable), isInline);
}