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

org.mentalog.LogException Maven / Gradle / Ivy

There is a newer version: 2.1.2
Show newest version
package org.mentalog;

public class LogException extends RuntimeException {

	protected final Throwable rootCause;

	public LogException() {
		super();
		this.rootCause = null;
	}

	public LogException(Throwable e) {
		super(getMsg(e), e);
		Throwable root = getRootCause(e);
		this.setStackTrace(root.getStackTrace());

		this.rootCause = root == this ? null : root;
	}

	public LogException(String msg) {
		super(msg);

		this.rootCause = null;
	}

	public LogException(String msg, Throwable e) {

		super(msg, e);
		Throwable root = getRootCause(e);
		this.setStackTrace(root.getStackTrace());

		this.rootCause = root == this ? null : root;
	}

	private static String getMsg(Throwable t) {

		Throwable root = getRootCause(t);

		String msg = root.getMessage();

		if (msg == null || msg.length() == 0) {

			msg = t.getMessage();

			if (msg == null || msg.length() == 0) {
				return root.getClass().getName();
			}
		}

		return msg;
	}

	private static Throwable getRootCause(Throwable t) {

		Throwable root = t.getCause();

		if (root == null) {
			return t;
		}

		while (root.getCause() != null) {

			root = root.getCause();
		}

		return root;

	}

	@Override
	public Throwable getCause() {

		return rootCause;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy