ExceptionUtils.java

1
package fr.sii.ogham.core.util;
2
3
import java.util.function.Predicate;
4
import java.util.regex.Pattern;
5
6
import org.slf4j.Logger;
7
import org.slf4j.LoggerFactory;
8
9
/**
10
 * Utility class for exceptions
11
 * 
12
 * @author Aurélien Baudet
13
 *
14
 */
15
public final class ExceptionUtils {
16
	private static final Logger LOG = LoggerFactory.getLogger(ExceptionUtils.class);
17
	private static final Pattern INDENT = Pattern.compile("^", Pattern.MULTILINE);
18
19
	/**
20
	 * Predicate that returns {@code true} if any cause in the exception stack
21
	 * matches the cause predicate.
22
	 * 
23
	 * <p>
24
	 * If a cause matches the predicate, then it return {@code true} immediately
25
	 * (skipping other checks).
26
	 * 
27
	 * @param error
28
	 *            the root error to analyze
29
	 * @param causePredicate
30
	 *            the predicate to apply to causes recursively
31
	 * @return true if the cause predicate returns true for at least one cause
32
	 *         in the exception stack
33
	 */
34
	public static boolean hasAnyCause(Throwable error, Predicate<Throwable> causePredicate) {
35
		Throwable cause = error;
36 1 1. hasAnyCause : negated conditional → RUN_ERROR
		while (cause != null) {
37 1 1. hasAnyCause : negated conditional → RUN_ERROR
			if (causePredicate.test(cause)) {
38 1 1. hasAnyCause : replaced boolean return with false for fr/sii/ogham/core/util/ExceptionUtils::hasAnyCause → RUN_ERROR
				return true;
39
			}
40
			cause = cause.getCause();
41
		}
42 1 1. hasAnyCause : replaced boolean return with true for fr/sii/ogham/core/util/ExceptionUtils::hasAnyCause → RUN_ERROR
		return false;
43
	}
44
45
	/**
46
	 * Checks whether the error has been raised due to a Java {@link Error}.
47
	 * {@link Error}s should not be ignored. For example, if there is a
48
	 * {@link OutOfMemoryError}, retrying may result in consuming more memory
49
	 * and totally crash the JVM or hang the system.
50
	 * 
51
	 * @param error
52
	 *            the raised error
53
	 * @return true if the error is fatal JVM error
54
	 */
55
	public static boolean fatalJvmError(Throwable error) {
56 2 1. fatalJvmError : replaced boolean return with false for fr/sii/ogham/core/util/ExceptionUtils::fatalJvmError → RUN_ERROR
2. fatalJvmError : replaced boolean return with true for fr/sii/ogham/core/util/ExceptionUtils::fatalJvmError → RUN_ERROR
		return error instanceof Error;
57
	}
58
59
	/**
60
	 * Generate a String based on the exception. Unlike default
61
	 * {@link Throwable#toString()} method, the {@link Throwable#getCause()} may
62
	 * also be included in the generated string (depending of logging
63
	 * configuration). If logger associated to this class is configured to
64
	 * {@code DEBUG} or {@code TRACE}, then the cause is included.
65
	 * 
66
	 * @param e
67
	 *            the error to convert to a string
68
	 * @return the error string
69
	 */
70
	public static String toString(Throwable e) {
71 1 1. toString : replaced return value with "" for fr/sii/ogham/core/util/ExceptionUtils::toString → RUN_ERROR
		return toString(e, "");
72
	}
73
74
	/**
75
	 * Generate a String based on the exception. Unlike default
76
	 * {@link Throwable#toString()} method, the {@link Throwable#getCause()} may
77
	 * also be included in the generated string (depending of logging
78
	 * configuration). If logger associated to this class is configured to
79
	 * {@code DEBUG} or {@code TRACE}, then the cause is included.
80
	 * 
81
	 * @param e
82
	 *            the error to convert to a string
83
	 * @param indentation
84
	 *            the initial indentation
85
	 * @return the error string
86
	 */
87
	public static String toString(Throwable e, String indentation) {
88
		String str = toThrowableString(e, indentation);
89
		if (LOG.isDebugEnabled() || LOG.isTraceEnabled()) {
90
			str += toCauseString(e, indentation + " ");
91
		}
92 1 1. toString : replaced return value with "" for fr/sii/ogham/core/util/ExceptionUtils::toString → RUN_ERROR
		return str;
93
	}
94
95
	private static String toThrowableString(Throwable e, String indentation) {
96 1 1. toThrowableString : negated conditional → RUN_ERROR
		if (e == null) {
97
			return "";
98
		}
99
		String s = e.getClass().getName();
100
		String message = e.getLocalizedMessage();
101 2 1. toThrowableString : replaced return value with "" for fr/sii/ogham/core/util/ExceptionUtils::toThrowableString → RUN_ERROR
2. toThrowableString : negated conditional → RUN_ERROR
		return indent(indentation, (message != null) ? (s + ": " + message) : s);
102
	}
103
104
	private static String toCauseString(Throwable e, String indentation) {
105 1 1. toCauseString : negated conditional → RUN_ERROR
		if (e == null) {
106
			return "";
107
		}
108
		Throwable cause = e.getCause();
109 1 1. toCauseString : negated conditional → RUN_ERROR
		if (cause == null) {
110
			return "";
111
		}
112 1 1. toCauseString : replaced return value with "" for fr/sii/ogham/core/util/ExceptionUtils::toCauseString → RUN_ERROR
		return "\n" + indent(indentation, toString(cause, indentation));
113
	}
114
115
	private static String indent(String indentation, String str) {
116 1 1. indent : replaced return value with "" for fr/sii/ogham/core/util/ExceptionUtils::indent → RUN_ERROR
		return INDENT.matcher(str).replaceAll(indentation);
117
	}
118
119
	private ExceptionUtils() {
120
		super();
121
	}
122
}

Mutations

36

1.1
Location : hasAnyCause
Killed by :
negated conditional → RUN_ERROR

37

1.1
Location : hasAnyCause
Killed by :
negated conditional → RUN_ERROR

38

1.1
Location : hasAnyCause
Killed by :
replaced boolean return with false for fr/sii/ogham/core/util/ExceptionUtils::hasAnyCause → RUN_ERROR

42

1.1
Location : hasAnyCause
Killed by :
replaced boolean return with true for fr/sii/ogham/core/util/ExceptionUtils::hasAnyCause → RUN_ERROR

56

1.1
Location : fatalJvmError
Killed by :
replaced boolean return with false for fr/sii/ogham/core/util/ExceptionUtils::fatalJvmError → RUN_ERROR

2.2
Location : fatalJvmError
Killed by :
replaced boolean return with true for fr/sii/ogham/core/util/ExceptionUtils::fatalJvmError → RUN_ERROR

71

1.1
Location : toString
Killed by :
replaced return value with "" for fr/sii/ogham/core/util/ExceptionUtils::toString → RUN_ERROR

92

1.1
Location : toString
Killed by :
replaced return value with "" for fr/sii/ogham/core/util/ExceptionUtils::toString → RUN_ERROR

96

1.1
Location : toThrowableString
Killed by :
negated conditional → RUN_ERROR

101

1.1
Location : toThrowableString
Killed by :
replaced return value with "" for fr/sii/ogham/core/util/ExceptionUtils::toThrowableString → RUN_ERROR

2.2
Location : toThrowableString
Killed by :
negated conditional → RUN_ERROR

105

1.1
Location : toCauseString
Killed by :
negated conditional → RUN_ERROR

109

1.1
Location : toCauseString
Killed by :
negated conditional → RUN_ERROR

112

1.1
Location : toCauseString
Killed by :
replaced return value with "" for fr/sii/ogham/core/util/ExceptionUtils::toCauseString → RUN_ERROR

116

1.1
Location : indent
Killed by :
replaced return value with "" for fr/sii/ogham/core/util/ExceptionUtils::indent → RUN_ERROR

Active mutators

Tests examined


Report generated by PIT 1.13.1