All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.honeybadger.reporter.dto.Causes Maven / Gradle / Ivy

There is a newer version: 2.1.2
Show newest version
package io.honeybadger.reporter.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.honeybadger.reporter.config.ConfigContext;

import java.io.Serializable;
import java.util.LinkedList;

/**
 * An ordered collection of chained exceptions.
 * @author Elijah Zupancic
 * @since 1.0.9
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@SuppressWarnings("JdkObsolete") // Reason: subtle interface change if we change LinkedList to ArrayList
public class Causes extends LinkedList implements Serializable {
    private static final long serialVersionUID = -5359764114506595006L;

    private static final int MAX_CAUSES = 100;

    public Causes(final ConfigContext config, final Throwable rootError) {
        if (rootError == null) {
            throw new IllegalArgumentException("Error can't be null");
        }
        addCauses(config, rootError);
    }

    void addCauses(final ConfigContext config, final Throwable rootError) {
        if (rootError.getCause() == null) return;

        Throwable lastCause = null;
        Throwable nextCause = rootError.getCause();

        int iterations = 0;

        do {
            addFirst(new Cause(config, nextCause));
            ++iterations;
            lastCause = nextCause;
            nextCause = nextCause.getCause();
        } while (null != nextCause &&
                // If we are in a simple circular reference, stop.
                !lastCause.equals(nextCause) &&
                // Since we could have multi-class circular ref we just check
                // for too big of a cause trace
                iterations <= MAX_CAUSES);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy