I am trying to get only detailMessage
from an Exception:
} catch (Exception e) {
logger.error(failed + e.getCause().getMessage()); }
The log file contains
"Unable to find element with name == filter:P_ClearButtonDFI (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 30.24 seconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 03:03:16' System info: host: 'OACVWMAX10006', ip: '10.29.19.64', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_79' *** Element info: {Using=name, value=filter:P_ClearButtonDFI} Session ID: 5149a0b8-475e-4d89-a9d6-e5115e773da8 Driver info: org.openqa.selenium.ie.InternetExplorerDriver Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=0, ignoreZoomSetting=false, enablePersistentHover=true, ie.ensureCleanSession=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss, version=9, ie.usePerProcessProxy=false, ignoreProtectedModeSettings=false, cssSelectorsEnabled=true, requireWindowFocus=false, initialBrowserUrl=http://localhost:21733/, handlesAlerts=true, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, ie.browserCommandLineSwitches=, takesScreenshot=true}]"
but I only want
Unable to find element with name == filter:P_ClearButtonDFI
One can use this method to get the detail message of exception as a string value. Return Value: This method returns the detailed message of this Throwable instance. Below programs demonstrate the getMessage () method of java.lang.Throwable Class // the getMessage () Method.
Example of displaying Exception message using toString () method, The getMessage () method of java.lang.Throwable class returns String. It gives only the reason for the exception. Most of the time we use the getMessage () method to display the message of the exception. Example to get exception message in Java using toString () method,
The getMessage() method of Throwable class is used to return a detailed message of the Throwable object which can also be null. One can use this method to get the detail message of exception as a string value. Syntax: Return Value: This method returns the detailed message of this Throwable instance.
Throwable getMessage () method in Java with Examples. The getMessage () method of Throwable class is used to return a detailed message of the Throwable object which can also be null. One can use this method to get the detail message of exception as a string value.
I don't think there is any elegant way to access it.
Of course you may use Java reflection to access the private field Throwable.detailMessage
but this far from being elegant :)
I work in Spring where exceptions often inherit org.springframework.core.NestedRuntimeException
which creates its message like
detailedMessage + "; nested exception is " + cause.detailedMessage
... + "; nested exception is " + cause.detailedMessage
I created a simple method
private String extractDetailedMessage(Throwable e) {
final String message = e.getMessage();
if (message == null) {
return "";
}
final int tailIndex = StringUtils.indexOf(message, "; nested exception is");
if (tailIndex == -1) {
return message;
}
return StringUtils.left(message, tailIndex);
}
You can easily modify it for your case :) I'm afraid you do not have anything nicer...
Here is a nice little Kotlin extension function for the same code shared by Honza Zidek
fun NestedRuntimeException.detailMessage(): String
= this.message.orEmpty().substringBefore("; nested exception is")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With