Gaurav Khurana
Gaurav Khurana

Reputation: 3901

How to get line number for error using exception in Groovy (soapui/readyapi)

  //Code 1 
  log.info undefined

When we run the code 1 , we get below error in soapui/readyapi as

enter image description here

please note :- Line number is visible in error message

However to avoid this alert , we used try/catch to print this, so the above code is amended to below as code 2

//code 2
try
{
log.info undefined
}
catch(Exception e)
{
log.info e  
}

when we run the code 2 we get below results

Mon Mar 19 15:04:16 IST 2018:INFO:groovy.lang.MissingPropertyException: No such property: undefined for class: Script6

Problem :- How can we see the line number where the problem is just like we are able to see in code1

Requirement :- Our exception block should be able to tell problem is in which line.

Since its a small code we are able to know, Sometimes the code is having 100+ lines and its difficult to know where the exception is

Upvotes: 3

Views: 5320

Answers (3)

Gaurav Khurana
Gaurav Khurana

Reputation: 3901

Thanks Chris and Jeremy for solving my problem.

I have used the below solution using Chris answer with all full respect to your answer

try
{
log.info undefined
}
catch(Exception e)
{
log.error "Exception = " + e    

String str= e.getStackTrace().toString()

def pattern = ( str =~ /groovy.(\d+)./   )

log.error " Error at line number = " + pattern[0][1]
}

The reason i am using that answer is i can avoid an import in all my scripts. I have used pattern matching to extract the line number as it always come like

(Script18.groovy:17),

so i have used the pattern /groovy.(\d+)./

Now i get both the exception details and line number

enter image description here

Upvotes: 4

Jeremy Hunt
Jeremy Hunt

Reputation: 727

Building on @tim_yates answer of using e.stackTrace.head().linenumber:

import org.codehaus.groovy.runtime.StackTraceUtils 

try {
    println undefined
} catch (Exception e) {
    StackTraceUtils.sanitize(e)
    e.stackTrace.head().lineNumber
}

Use sanitize() on your Exception to remove all the weird Groovy internal stuff from the stack trace for your Exception. Otherwise when you look at the first StackTraceElement, it probably won't be the one you want.

deepSanitize() is the same, but also applies the transform to all the nested Exceptions if there are any.

Upvotes: 7

Chris Adams
Chris Adams

Reputation: 1454

You can use log.info e.getStackTrace().toString(); to get the full stack trace.

However, it'll be hard to pick out the issue. Here's my Groovy script....

try
{
log.info undefined
}
catch(Exception e)
{
log.info e.getStackTrace().toString();
}

Here's the trace....

Mon Mar 19 17:15:20 GMT 2018:INFO:[org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50), org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49), org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231), Script21.run(Script21.groovy:3), com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:100), com.eviware.soapui.support.scripting.groovy.SoapUIProGroovyScriptEngineFactory$SoapUIProGroovyScriptEngine.run(SourceFile:89), com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:154), com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$1.run(GroovyScriptStepDesktopPanel.java:277), java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source), java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source), java.lang.Thread.run(Unknown Source)]

Note- the emboldened part. It's line 3 and that is what I expected. However, SoapUI must use internal numbering for scripts as I called the script "Dummy Groovy Script" and the stack trace says "Script21".

Anyhow, I do think you ought look at your Groovy script, 100+ lines in a Try seems a bit much and as you pint out, it's difficult to see the issue.

I'd suggest breaking it down into functions, or even better, call an Java class external to SoapUI, that has nice well-defined functions.

The SmartBear site describes how this can be done. Plus, it remove a lot of the bloat from the SoapUI project file.

Upvotes: 1

Related Questions