Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve exception details when karate.call fails in js script #2216

Closed
shrik18 opened this issue Dec 13, 2022 · 6 comments
Closed

improve exception details when karate.call fails in js script #2216

shrik18 opened this issue Dec 13, 2022 · 6 comments
Assignees
Milestone

Comments

@shrik18
Copy link
Contributor

shrik18 commented Dec 13, 2022

today when karate.call is made using js function it throws generic exception with no details

       * def fun =
          """
		function() {
		  		  try {
		    			  karate.call('classpath:com/intuit/karate/test/js-test-abstract.feature')
		  			   }catch (e){
		    				return(e);
		  			  }
		}
	"""
    * def e = fun()
    * print 'exception>>>',e

[print] exception>>> com.intuit.karate.KarateException: http call failed after 4519 milliseconds for url: https://localhost:8080/abcd/xyz/3.1
classpath:com/intuit/karate/test/js-test-abstract.feature:15

The actual exception is suppressed

19:33:57.062 [main] ERROR com.intuit.karate - javax.net.ssl.SSLException: Unsupported or unrecognized SSL message, http call failed after 425 milliseconds for url: https://localhost:8080/abcd/xyz/3.1

It will help adding these details to serve odd cases like https://stackoverflow.com/questions/55095314/

@ptrthomas
Copy link
Member

this is low priority for us, anyone is welcome to contribute a PR

@shrik18
Copy link
Contributor Author

shrik18 commented Dec 13, 2022

I did some homework and seems this is the class tha would need change:

public static Result failed(long nanos, Throwable error, Step step, StepRuntime.MethodMatch matchingMethod) {
String message = error.getMessage();
if (message == null) {
message = error + ""; // make sure we show something meaningful
}
error = new KarateException(message + "\n" + step.getDebugInfo());
StackTraceElement[] newTrace = new StackTraceElement[]{
new StackTraceElement("<feature>", ": " + step.getPrefix() + " " + step.getText() + " ", step.getDebugInfo(), step.getLine())
};
error.setStackTrace(newTrace);
return new Result(FAILED, nanos, error, false, matchingMethod);
}

Change>>

    String message = error.getMessage();
    String errCause = error.getCause().getMessage();
    if (message == null) {
            message = error + ""; // make sure we show something meaningful
        }
    error = new KarateException(message + "\n" + step.getDebugInfo() + "\n" + errCause);

Output >>>>
19:31:17.003 [main] INFO com.intuit.karate - [print] exception>>> com.intuit.karate.KarateException: http call failed after 4610 milliseconds for url: https://localhost:8080/sapi/hws/3.1
classpath:com/intuit/karate/test/js-test-abstract.feature:15
java.lang.RuntimeException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:8080 [localhost/127.0.0.1,

@ptrthomas
Copy link
Member

@shrik18 that code has evolved over 6 years to keep the logs free of noise. I think getCause() can return null in some cases. anyway, a PR and thorough testing (and observation) is required.

@shrik18
Copy link
Contributor Author

shrik18 commented Dec 14, 2022

@ptrthomas agreed due diligence is required and indeed getCause() possibly can return null.
I have another solution where I can make this change.

try {
response = requestBuilder.client.invoke(httpRequest);
} catch (Exception e) {
long endTime = System.currentTimeMillis();
long responseTime = endTime - startTime;
String message = "http call failed after " + responseTime + " milliseconds for url: " + httpRequest.getUrl();
logger.error(e.getMessage() + ", " + message);
if (logger.isTraceEnabled()) {
String stacktrace = StringUtils.throwableToString(e);
if (stacktrace != null) {
logger.trace(stacktrace);
}
}
if (perfEventName != null) {
PerfEvent pe = new PerfEvent(startTime, endTime, perfEventName, 0);
capturePerfEvent(pe); // failure flag and message should be set by logLastPerfEvent()
}
throw new KarateException(message, e);
}

Line 611 : this is where actual exception is added to cause. I can ammend this to
throw new KarateException(message +"\n" + e.getMessage(), e)

This way we are pretty much sure only http failures exceptions are added to message, and no risk of null pointer

Its up to you to choose the solution and I will raise a PR.

@ptrthomas
Copy link
Member

@shrik18 yes that sounds good

shrik18 added a commit to shrik18/karate that referenced this issue Dec 14, 2022
 Added http exception message to karate exception so it appears in Result as well 
 karatelabs#2216
@ptrthomas ptrthomas added this to the 1.4.0 milestone Dec 14, 2022
@ptrthomas
Copy link
Member

1.4.0 released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants