Join exception chains together

Returns the last exception or NULL if no exceptions were given.
498591
static function joinChains(\Throwable ...$nodes): ?\Throwable
    {
        $lastNodeIndex = count($nodes) - 1;

        if ($lastNodeIndex > 0) {
            // iterate over all but the last node
            for ($i = 0; $i < $lastNodeIndex; ++$i) {
                // find initial node of the next chain
                $initialNode = $nodes[$i + 1];
                $hashMap = [];
                while (($previousNode = $initialNode->getPrevious()) && !isset($hashMap[$hash = spl_object_hash($previousNode)])) {
                    $initialNode = $previousNode;
                }

                // connect end of the current chain (= current node)
                // to the initial node of the next chain
                $previousProperty = new \ReflectionProperty(
                    ($parents = class_parents($initialNode)) ? end($parents) : $initialNode,
                    'previous'
                );

                $previousProperty->setAccessible(true);
                $previousProperty->setValue($initialNode, $nodes[$i]);
            }
        }

        return $nodes[$lastNodeIndex] ?? null;
    }
1138919
public static Throwable getRootCause(Throwable exception) {
    Assert.notNull(exception);
    return exception.getCause() == null ? exception : getRootCause(exception.getCause());
  }
1039699
JournalSegment<E> getLastSegment() {
    assertOpen();
    Map.Entry<Long, JournalSegment<E>> segment = segments.lastEntry();
    return segment != null ? segment.getValue() : null;
  }
1026161
public static String getTrimmedStackTrace(Throwable exception) {
        List<String> trimmedStackTraceLines = getTrimmedStackTraceLines(exception);
        if (trimmedStackTraceLines.isEmpty()) {
            return getFullStackTrace(exception);
        }

        StringBuilder result = new StringBuilder(exception.toString());
        appendStackTraceLines(trimmedStackTraceLines, result);
        appendStackTraceLines(getCauseStackTraceLines(exception), result);
        return result.toString();
    }
8749
private static Throwable getLastArgumentIfThrowable(Object... arguments) {
		if (ArrayUtil.isNotEmpty(arguments) && arguments[arguments.length - 1] instanceof Throwable) {
			return (Throwable) arguments[arguments.length - 1];
		} else {
			return null;
		}
	}
1369284
@Override
  public void onException(Throwable failureCause) {
    Preconditions.checkNotNull(failureCause, "failureCause");
    Status status = Status.UNAVAILABLE.withCause(failureCause);
    startGoAway(0, ErrorCode.INTERNAL_ERROR, status);
  }
691719
public final Throwable getException() {
        int s = status & DONE_MASK;
        return ((s >= NORMAL)    ? null :
                (s == CANCELLED) ? new CancellationException() :
                getThrowableException());
    }
355520
public Class getExceptionClass()
    {
    	Class exceptionClass = null;
        Throwable rootCause = this;
        
        do{
            Throwable temp = ((ServletException)rootCause).getRootCause();
            if(temp != null) {
                rootCause = temp;
            }
            else{
                break;
            }
        }while (rootCause instanceof ServletException);

        // rootCause must be non-null (it started as "this" and was then NEVER set to null by the loop above
        exceptionClass = rootCause.getClass();

        return exceptionClass;
    }
29607
private void clearExceptionalCompletion() {
        int h = System.identityHashCode(this);
        final ReentrantLock lock = exceptionTableLock;
        lock.lock();
        try {
            ExceptionNode[] t = exceptionTable;
            int i = h & (t.length - 1);
            ExceptionNode e = t[i];
            ExceptionNode pred = null;
            while (e != null) {
                ExceptionNode next = e.next;
                if (e.get() == this) {
                    if (pred == null)
                        t[i] = next;
                    else
                        pred.next = next;
                    break;
                }
                pred = e;
                e = next;
            }
            expungeStaleExceptions();
            status = 0;
        } finally {
            lock.unlock();
        }
    }
610227
public static String join(Collection<?> objectsToJoin, String separator) {
        if ((objectsToJoin != null) && !objectsToJoin.isEmpty()) {
            StringBuilder builder = new StringBuilder();
            for (Object object : objectsToJoin) {
                builder.append(object);
                builder.append(separator);
            }
            // Remove the last separator
            return builder.substring(0, builder.length() - separator.length());
        } else {
            return EMPTY;
        }
    }
356154
public static <T, X extends RuntimeException> T ifNonNullThrowOrElse(Supplier<X> exceptionSupplier,
            Supplier<T> provider) {
        if (Objects.nonNull(exceptionSupplier)) {
            throw exceptionSupplier.get();
        } else {
            return provider.get();
        }
    }
1205483
private RetriesExhaustedWithDetailsException getExceptions()
      throws RetriesExhaustedWithDetailsException {
    if (!hasExceptions.get()) {
      return null;
    }

    ArrayList<MutationException> mutationExceptions = null;
    synchronized (globalExceptions) {
      hasExceptions.set(false);
      if (globalExceptions.isEmpty()) {
        return null;
      }

      mutationExceptions = new ArrayList<>(globalExceptions);
      globalExceptions.clear();
    }

    List<Throwable> problems = new ArrayList<>(mutationExceptions.size());
    ArrayList<String> hostnames = new ArrayList<>(mutationExceptions.size());
    List<Row> failedMutations = new ArrayList<>(mutationExceptions.size());

    if (!mutationExceptions.isEmpty()) {
      LOG.warn("Exception occurred in BufferedMutator", mutationExceptions.get(0).throwable);
    }
    for (MutationException mutationException : mutationExceptions) {
      problems.add(mutationException.throwable);
      failedMutations.add(mutationException.mutation);
      hostnames.add(host);
      LOG.debug("Exception occurred in BufferedMutator", mutationException.throwable);
    }

    return new RetriesExhaustedWithDetailsException(problems, failedMutations, hostnames);
  }
56923
private static Throwable getInnerException(Throwable exception) {// NOPMD

		final Throwable tmpException = exception;

		exception = getExceptionCauseUsingWellKnownTypes(tmpException);
		if (exception == null) {
			for (CauseMethodName methodName : CauseMethodName.values()) {
				exception = getExceptionCauseUsingMethodName(tmpException, methodName);
				if (exception != null) {
					break;
				}
			}
		}

		return exception;
	}
772593
public void terminateConnectionsAssociatedWithChain(String chainName) throws Exception {

        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            Tr.entry(this, tc, "terminateConnectionsAssociatedWithChain", chainName);

        synchronized (endPointToGroupMap) {

            try {
                final HashMap<EndPointDescriptor, ConnectionDataGroup> endPointToGroupMapClone = (HashMap<EndPointDescriptor, ConnectionDataGroup>) endPointToGroupMap.clone();
                Iterator<EndPointDescriptor> it = endPointToGroupMapClone.keySet().iterator();
                while (it.hasNext()) {
                    EndPointDescriptor ed = it.next();

                    if ((((HostPortEndPointDescriptor) ed).chainName).equals(chainName)) {

                        ConnectionDataGroup cdGroup = endPointToGroupMapClone.get(ed);

                        final List connectionData = cdGroup.getConnections();

                        for (final Iterator connectionDataIterator = connectionData.iterator(); connectionDataIterator.hasNext();)
                        {
                            final ConnectionData thisConnectionData = (ConnectionData) connectionDataIterator.next();
                            final OutboundConnection oc = thisConnectionData.getConnection();
                            //close all the conservations associated with the OC
                            Conversation[] conv = oc.getConversations();
                            for (Conversation c : conv) {
                                try {
                                    c.fastClose();
                                } catch (Exception e) {//Don't let the exception mess up closing other remaining conservations
                                    if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled())
                                        SibTr.debug(tc, "Error while fast closing the conversation", e);
                                }

                            }
                            try {
                                // terminate the physical connection
                                purgeFromInvalidateImpl(oc, false);
                            } catch (Exception e) {//Don't let the exception mess up closing other remaining connection
                                if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled())
                                    SibTr.debug(tc, "Error while purging the physical connection", e);
                            }
                        }

                        try {
                            // destroy the OutboundVirtualConnection associated with the chainName
                            cdGroup.getNetworkConnectionFactory().getOutboundVirtualConFactory().destroy();
                        } catch (Exception e) {//Don't let the exception mess up closing other remaining connection
                            if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled())
                                SibTr.debug(tc, "Error while destroying the outbound virtual connection", e);
                        }
                        // in fastClose() the entry is removed but still we do a remove 
                        endPointToGroupMap.remove(ed);

                    }
                }

            } catch (Exception e) {
                throw e;
            } finally {
                if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
                    Tr.exit(this, tc, "terminateConnectionsAssociatedWithChain");
            }

        }
    }
1333748
public String getMessage()
  {

    String lastMessage = super.getMessage();
    Throwable exception = m_exception;

    while (null != exception)
    {
      String nextMessage = exception.getMessage();

      if (null != nextMessage)
        lastMessage = nextMessage;

      if (exception instanceof TransformerException)
      {
        TransformerException se = (TransformerException) exception;
        Throwable prev = exception;

        exception = se.getException();

        if (prev == exception)
          break;
      }
      else
      {
        exception = null;
      }
    }

    return (null != lastMessage) ? lastMessage : "";
  }
973990
public void noExceptionThrown() {
    Throwable thrown = getSpecificationContext().getThrownException();
    if (thrown == null) return;
    throw new UnallowedExceptionThrownError(null, thrown);
  }
253163
private static void expungeStaleExceptions() {
        for (Object x; (x = exceptionTableRefQueue.poll()) != null;) {
            if (x instanceof ExceptionNode) {
                ForkJoinTask<?> key = ((ExceptionNode)x).get();
                ExceptionNode[] t = exceptionTable;
                int i = System.identityHashCode(key) & (t.length - 1);
                ExceptionNode e = t[i];
                ExceptionNode pred = null;
                while (e != null) {
                    ExceptionNode next = e.next;
                    if (e == x) {
                        if (pred == null)
                            t[i] = next;
                        else
                            pred.next = next;
                        break;
                    }
                    pred = e;
                    e = next;
                }
            }
        }
    }