Skip to content

Commit 08eb121

Browse files
Vladimir.ShapkinVladimir.Shapkin
Vladimir.Shapkin
authored and
Vladimir.Shapkin
committed
Implementation of
yegor256#1715 Missing analog for ExceptionUtils: The most useful thing to get rootCause Exception. https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/exception/ExceptionUtils.html
1 parent 665cf8a commit 08eb121

File tree

8 files changed

+397
-0
lines changed

8 files changed

+397
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017-2024 Yegor Bugayenko
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package org.cactoos.exception;
25+
26+
import java.util.Iterator;
27+
import org.cactoos.Scalar;
28+
import org.cactoos.iterator.IteratorOfStackTrace;
29+
import org.cactoos.iterator.TailOf;
30+
31+
/**
32+
* Return root cause exception.
33+
*
34+
* <p>There is no thread-safety guarantee.
35+
*
36+
* @since 0.56
37+
*/
38+
public final class RootCause implements Scalar<Throwable> {
39+
40+
/**
41+
* Iterator for exceptions.
42+
*/
43+
private final Scalar<Iterator<Throwable>> itr;
44+
45+
/**
46+
* Ctor.
47+
* @param exc The exception to iterate.
48+
*/
49+
public RootCause(final Throwable exc) {
50+
this(new IteratorOfStackTrace(exc));
51+
}
52+
53+
/**
54+
* Ctor.
55+
* @param iter The exception Iterator.
56+
*/
57+
public RootCause(final Iterator<Throwable> iter) {
58+
this(() -> new TailOf<>(1, iter));
59+
}
60+
61+
/**
62+
* Ctor.
63+
* @param itr Decorated iterator.
64+
*/
65+
public RootCause(final Scalar<Iterator<Throwable>> itr) {
66+
this.itr = itr;
67+
}
68+
69+
@Override
70+
public Throwable value() throws Exception {
71+
return this.itr.value().next();
72+
}
73+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017-2024 Yegor Bugayenko
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
/**
26+
* Exceptions.
27+
*
28+
* @since 0.56
29+
*/
30+
package org.cactoos.exception;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017-2024 Yegor Bugayenko
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package org.cactoos.iterable;
25+
26+
import org.cactoos.iterator.IteratorOfStackTrace;
27+
28+
/**
29+
* Iterable of exception.
30+
*
31+
* <p>There is no thread-safety guarantee.
32+
*
33+
* @since 0.56
34+
*/
35+
public final class IterableOfStackTrace extends IterableEnvelope<Throwable> {
36+
37+
/**
38+
* Ctor.
39+
* @param exc The exception to iterate.
40+
*/
41+
public IterableOfStackTrace(final Throwable exc) {
42+
super(new IterableOf<>(() -> new IteratorOfStackTrace(exc)));
43+
}
44+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017-2024 Yegor Bugayenko
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package org.cactoos.iterator;
25+
26+
import java.util.Iterator;
27+
import java.util.NoSuchElementException;
28+
29+
/**
30+
* {@link Iterator} that returns an exception from the exception stack trace.
31+
*
32+
* <p>There is no thread-safety guarantee.</p>
33+
*
34+
* @since 0.56
35+
*/
36+
public final class IteratorOfStackTrace implements Iterator<Throwable> {
37+
38+
/**
39+
* The exception to iterate.
40+
*/
41+
private Throwable exception;
42+
43+
/**
44+
* Ctor.
45+
* @param exc The exception to iterate.
46+
*/
47+
public IteratorOfStackTrace(final Throwable exc) {
48+
this.exception = exc;
49+
}
50+
51+
@Override
52+
public boolean hasNext() {
53+
return this.exception.getCause() != null;
54+
}
55+
56+
@Override
57+
public Throwable next() {
58+
if (this.hasNext()) {
59+
this.exception = this.exception.getCause();
60+
return this.exception;
61+
}
62+
throw new NoSuchElementException("The iterator doesn't have item");
63+
}
64+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017-2024 Yegor Bugayenko
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package org.cactoos.exception;
25+
26+
import org.hamcrest.core.IsEqual;
27+
import org.junit.jupiter.api.Test;
28+
import org.llorllale.cactoos.matchers.Assertion;
29+
30+
/**
31+
* Test Case for {@link RootCause}.
32+
*
33+
* @since 0.56
34+
*/
35+
final class RootCauseTest {
36+
37+
@Test
38+
void rootCauseTest() throws Exception {
39+
final Throwable inner = new Throwable();
40+
final RootCause exc = new RootCause(new Throwable(new Throwable(inner)));
41+
new Assertion<>(
42+
"Should return inner exception.",
43+
exc.value(),
44+
new IsEqual<>(inner)
45+
).affirm();
46+
}
47+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017-2024 Yegor Bugayenko
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
/**
26+
* Exception, test.
27+
*
28+
* @since 0.56
29+
*/
30+
package org.cactoos.exception;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017-2024 Yegor Bugayenko
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package org.cactoos.iterable;
25+
26+
import org.hamcrest.Matchers;
27+
import org.junit.jupiter.api.Test;
28+
import org.llorllale.cactoos.matchers.Assertion;
29+
30+
/**
31+
* Test Case for {@link IterableOfStackTrace}.
32+
* @since 0.56
33+
*/
34+
final class IterableOfStackTraceTest {
35+
36+
@Test
37+
void exceptionIterableTest() {
38+
final Throwable expected = new Throwable();
39+
new Assertion<>(
40+
"Must get the expected exception.",
41+
new IterableOfStackTrace(new Throwable(expected)),
42+
Matchers.hasItem(expected)
43+
).affirm();
44+
}
45+
}

0 commit comments

Comments
 (0)