Skip to content

Commit

Permalink
strip stack trace elements
Browse files Browse the repository at this point in the history
  • Loading branch information
Starlight220 committed Apr 4, 2024
1 parent 7573b02 commit a45a463
Showing 1 changed file with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
Expand Down Expand Up @@ -637,18 +638,38 @@ public void requireNotComposed(Command... commands) {
for (var command : commands) {
var exception = m_composedCommands.getOrDefault(command, null);
if (exception != null) {
exception.setStackTrace(stripFrameworkStackElements(exception.getStackTrace()));
var buffer = new StringWriter();
var writer = new PrintWriter(buffer);
writer.println(
"Commands that have been composed may not be added to another composition or scheduled "
+ "individually!");
writer.println(exception.getMessage());
exception.printStackTrace(writer);
throw new IllegalArgumentException(buffer.toString());
var thrownException = new IllegalArgumentException(buffer.toString());
thrownException.setStackTrace(stripFrameworkStackElements(thrownException.getStackTrace()));
throw thrownException;
}
}
}

/**
* Strip all leading stack trace elements that are in the framework package.
*
* @param stacktrace the original stacktrace
* @return the stacktrace stripped of leading elements in the
* edu.wpi.first.wpilibj2.command package.
*/
private StackTraceElement[] stripFrameworkStackElements(StackTraceElement[] stacktrace) {
int i = 0;
for (; i < stacktrace.length; i++) {
if (stacktrace[0].getClassName().startsWith("edu.wpi.first.wpilibj2.command.")) {
continue;
}
break;
}
return Arrays.copyOfRange(stacktrace, i, stacktrace.length);
}

/**
* Requires that the specified commands have not already been added to a composition.
*
Expand Down

0 comments on commit a45a463

Please sign in to comment.