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

org.hibernate.search.util.impl.Throwables Maven / Gradle / Ivy

There is a newer version: 5.11.12.Final
Show newest version
/*
 * Hibernate Search, full-text search for your domain model
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later
 * See the lgpl.txt file in the root directory or .
 */
package org.hibernate.search.util.impl;

import org.hibernate.search.exception.AssertionFailure;

/**
 * Throwable-related utils.
 *
 * @author Yoann Rodiere
 */
public final class Throwables {

	private Throwables() {
	}

	public static RuntimeException expectRuntimeException(Throwable throwable) {
		if ( throwable instanceof RuntimeException ) {
			return (RuntimeException) throwable;
		}
		else if ( throwable instanceof Error ) {
			// Do not wrap errors: it would be "unreasonable" according to the Error javadoc
			throw (Error) throwable;
		}
		else if ( throwable == null ) {
			throw new AssertionFailure( "Null throwable - there is probably a bug" );
		}
		else {
			throw new AssertionFailure( "Unexpected throwable type - there is probably a bug", throwable );
		}
	}

	public static Exception expectException(Throwable throwable) {
		if ( throwable instanceof Exception ) {
			return (Exception) throwable;
		}
		else if ( throwable instanceof Error ) {
			// Do not wrap errors: it would be "unreasonable" according to the Error javadoc
			throw (Error) throwable;
		}
		else if ( throwable == null ) {
			throw new AssertionFailure( "Null throwable - there is probably a bug" );
		}
		else {
			throw new AssertionFailure( "Unexpected throwable type - there is probably a bug", throwable );
		}
	}

	public static  T combine(T throwable, T otherThrowable) {
		T toThrow = throwable;
		if ( otherThrowable != null ) {
			if ( toThrow != null ) {
				toThrow.addSuppressed( otherThrowable );
			}
			else {
				toThrow = otherThrowable;
			}
		}
		return toThrow;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy