1

I'm using a third party library that returns Exception information in the form of a String (not an Exception object). I want to extract certain parts of the String stack trace to show the user in a GUI.

Two examples:

com.sdf.configure.model.change.InvalidChangeException: transformed change validation failed
    at com.sdf.configure.model.change.SimpleConfigChanger.performChange(SimpleConfigChanger.java:129)
    at com.sdf.portal.service.QosPolicyServiceImpl.applyPolicySnapshot(QosPolicyServiceImpl.java:99)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.referentia.commons.distributed.remoting.jms.JmsServiceExporter$1$1.run(JmsServiceExporter.java:189)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)
Caused by: com.sdf.configure.model.change.InvalidChangeException: can't add input service policy: policy has queueing
    at com.sdf.configure.model.validate.RuleBasedValidator$RuleBasedChangeValidator.checkValidChange(RuleBasedValidator.java:5188)
    at com.sdf.configure.model.validate.AbstractContextChangeValidator.checkValidChange(AbstractContextChangeValidator.java:304)
    at com.sdf.configure.model.validate.AbstractContextChangeValidator.checkValidChange(AbstractContextChangeValidator.java:140)
    at com.sdf.configure.model.validate.RuleBasedValidator.checkValidChange(RuleBasedValidator.java:1039)
    at com.sdf.configure.model.change.SimpleConfigChanger.performChange(SimpleConfigChanger.java:126)
    ... 12 more

I want to extract the text: can't add input service policy: policy has queueing

com.sdf.configure.model.change.InvalidChangeException: transformed change validation failed
    at com.sdf.configure.model.change.SimpleConfigChanger.performChange(SimpleConfigChanger.java:129)
    at com.sdf.portal.service.QosPolicyServiceImpl.applyPolicySnapshot(QosPolicyServiceImpl.java:99)
    at sun.reflect.GeneratedMethodAccessor139.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sdf.commons.distributed.remoting.jms.JmsServiceExporter$1$1.run(JmsServiceExporter.java:189)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)
Caused by: com.sdf.configure.model.change.InvalidChangeException: compound change component validation failed
    at com.sdf.configure.model.validate.AbstractContextChangeValidator.checkValidChange(AbstractContextChangeValidator.java:1309)
    at com.sdf.configure.model.validate.AbstractContextChangeValidator.checkValidChange(AbstractContextChangeValidator.java:361)
    at com.sdf.configure.model.validate.AbstractContextChangeValidator.checkValidChange(AbstractContextChangeValidator.java:140)
    at com.sdf.configure.model.validate.RuleBasedValidator.checkValidChange(RuleBasedValidator.java:1039)
    at com.sdf.configure.model.change.SimpleConfigChanger.performChange(SimpleConfigChanger.java:126)
    ... 11 more
Caused by: com.sdf.configure.model.change.InvalidChangeException: can't add input service policy: policy has shaping
    at com.sdf.configure.model.validate.RuleBasedValidator$RuleBasedChangeValidator.checkValidChange(RuleBasedValidator.java:5180)
    at com.sdf.configure.model.validate.AbstractContextChangeValidator.checkValidChange(AbstractContextChangeValidator.java:304)
    at com.sdf.configure.model.validate.AbstractContextChangeValidator.checkValidChange(AbstractContextChangeValidator.java:1306)
    ... 15 more

I want to extract the text:

compound change component validation failed

can't add input service policy: policy has shaping

Any elegant way to do this? The only way to find the parts I want to extract seems to be that the pieces I want to extract are always preceded by:

Caused by: com.sdf.configure.model.change.InvalidChangeException:

3 Answers 3

2

This regex will handle that...

Original Regex: ^Caused\sby:\s\S+:(.+)$

as a Java String: "^Caused\\sby:\\s\\S+:(.+)$"

You can validate this easily with this online tool.

You'll need to include the MULTILINE option

Example code snippit:

String regex = "^Caused\\sby:\\s\\S+:(.+)$";
Pattern p = Pattern.compile(regex, Pattern.MULTILINE);

Matcher m = p.matcher(exceptionString);
while (m.find()) {
    String outputThis = m.group(1);
}
Sign up to request clarification or add additional context in comments.

1 Comment

WOW... someday I need to learn how to use regex.. That works like a champ.
1

Use string.indexOf to get the location of the invalid change exception, then use stirng.index of "at com." to get the index of the next part of the stack trace, tehn grab everything between the two indexes adjusted for the exception text string length as an offset

Comments

1

Use:

StringUtils.substringsBetween(searchMe, start, end);

Like this:

String[] causes = StringUtils.substringsBetween(exceptionMessage, 
                        "Caused by: com.sdf.configure.model.change.InvalidChangeException:", 
                        "at com.sdf");

This will give you an array of text delimited by the 2nd and 3rd parameters.

Here's the JavaDoc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.