Skip to content

Commit 603b419

Browse files
committed
♻️ refactor: update codebase #3
1 parent 73de0be commit 603b419

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

plugin/src/main/groovy/org/unify4j/common/Exception4j.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,27 @@ public static Throwable getDeepestException(Throwable e) {
2929
}
3030
return e;
3131
}
32+
33+
/**
34+
* Retrieves the class name and line number from the stack trace of the provided
35+
* exception. This method is used to extract and return a string representation of
36+
* where the exception occurred, specifically the class and line number of the first
37+
* element in the stack trace.
38+
* <p>
39+
* If the exception or its stack trace is null or empty, the method returns an empty
40+
* string or "Unknown" as a fallback.
41+
*
42+
* @param e The {@link Exception} from which to extract the stack trace information.
43+
* @return A {@link String} representing the class name and line number in the format
44+
* "ClassName:LineNumber", or "Unknown" if the stack trace is not available.
45+
*/
46+
public static String getClassLine(Exception e) {
47+
if (e == null) {
48+
return "";
49+
}
50+
StackTraceElement[] trace = e.getStackTrace();
51+
return (trace != null && trace.length > 0)
52+
? trace[0].getClassName() + ":" + trace[0].getLineNumber()
53+
: "Unknown";
54+
}
3255
}

0 commit comments

Comments
 (0)