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

com.softicar.platform.common.core.exception.MultiExceptionMessageBuilder Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.core.exception;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collection;
import java.util.List;
import java.util.TreeMap;
import java.util.stream.Collectors;

class MultiExceptionMessageBuilder {

	private final Collection exceptions;

	public MultiExceptionMessageBuilder(Collection exceptions) {

		this.exceptions = exceptions;
	}

	public String buildMessage() {

		var buffer = new StringWriter();
		try (var printer = new PrintWriter(buffer)) {
			Collection> exceptionLists = getExceptionLists();
			if (!exceptionLists.isEmpty()) {
				printer.println();
				printer.println("Gathered a total of %s exceptions with %s different stack traces.".formatted(exceptions.size(), exceptionLists.size()));
				printer.println();
			}

			int index = 0;
			for (List exceptionList: exceptionLists) {
				printer.println("-------------------- %s exceptions with stack trace #%s --------------------".formatted(exceptionList.size(), index));
				printer.println();
				for (Throwable exception: exceptionList) {
					exception.printStackTrace(printer);
					printer.println();
				}
				index++;
			}
		}
		return buffer.toString();
	}

	private Collection> getExceptionLists() {

		return exceptions//
			.stream()
			.collect(Collectors.groupingBy(this::getExceptionStackTraceKey, TreeMap::new, Collectors.toList()))
			.values();
	}

	private String getExceptionStackTraceKey(Throwable exception) {

		return List.of(exception.getStackTrace()).toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy