-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Maven exception handling / fixes #2334
- Loading branch information
1 parent
b818b09
commit 3bcafba
Showing
71 changed files
with
1,623 additions
and
758 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
rewrite-core/src/main/java/org/openrewrite/internal/ExceptionUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.internal; | ||
|
||
import java.util.StringJoiner; | ||
|
||
public class ExceptionUtils { | ||
/** | ||
* Shorten a stack trace to the first invocation of a particular class. | ||
* | ||
* @param t The original exception | ||
* @param until Cut the stack trace when it reaches a method in this class. | ||
* @return The sanitized stack trace | ||
*/ | ||
public static String sanitizeStackTrace(Throwable t, Class<?> until) { | ||
StringJoiner sanitized = new StringJoiner("\n"); | ||
Throwable cause = t instanceof RecipeRunException ? t.getCause() : t; | ||
sanitized.add(cause.getClass().getName() + ": " + cause.getLocalizedMessage()); | ||
|
||
int i = 0; | ||
for (StackTraceElement stackTraceElement : cause.getStackTrace()) { | ||
if (stackTraceElement.getClassName().equals(until.getName())) { | ||
break; | ||
} | ||
if (i++ >= 8) { | ||
sanitized.add(" ..."); | ||
break; | ||
} | ||
sanitized.add(" " + stackTraceElement); | ||
} | ||
return sanitized.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
rewrite-core/src/main/java/org/openrewrite/internal/Throwing.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.internal; | ||
|
||
import java.util.function.Consumer; | ||
|
||
public final class Throwing { | ||
private Throwing() { | ||
} | ||
|
||
public static <T> Consumer<T> rethrow(ThrowingConsumer<T> consumer) { | ||
return consumer; | ||
} | ||
|
||
/** | ||
* The compiler sees the signature with the throws T inferred to a RuntimeException type, so it | ||
* allows the unchecked exception to propagate. {@see http://www.baeldung.com/java-sneaky-throws}. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static <E extends Throwable> void sneakyThrow(Throwable e) throws E { | ||
throw (E) e; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
rewrite-core/src/main/java/org/openrewrite/internal/ThrowingConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.internal; | ||
|
||
import java.util.function.Consumer; | ||
|
||
@FunctionalInterface | ||
public interface ThrowingConsumer<T> extends Consumer<T> { | ||
|
||
@Override | ||
default void accept(final T e) { | ||
try { | ||
accept0(e); | ||
} catch (Throwable ex) { | ||
Throwing.sneakyThrow(ex); | ||
} | ||
} | ||
|
||
void accept0(T e) throws Throwable; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.