Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Exception: get detailMessage only

Tags:

java

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

like image 396
Anton_Selenium Avatar asked Nov 20 '15 11:11

Anton_Selenium


People also ask

How to get the detail message of an exception in Java?

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.

How to get exception message in Java using ToString() 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,

How to get the detail message of an exception in throwable?

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.

What is the use of GetMessage in Java?

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.


2 Answers

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...

like image 94
Honza Zidek Avatar answered Sep 24 '22 17:09

Honza Zidek


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")
like image 31
Bahadır Yağan Avatar answered Sep 26 '22 17:09

Bahadır Yağan