How to handle exception or error occured due to sending request to blocked site

Hi,

I am trying to check if an url is accessible or not in Katalon Studio V9.6 and for that i’m using the below method:

try {
	status = getResponseStatus(url)
} catch(Exception ex) {
	println('in catch')
}

String getResponseStatus(String url) {
	String status = '???'
	println("requesting on URL: " + url)
	if (url != null && url.startsWith('http')) {
		RequestObject req = new RequestObject('check if this url is reachable')
		req.setServiceType('REST')
		req.setRestUrl(url)
		req.setRestRequestMethod('GET')
		// Send the request and get the response
		try {
			ResponseObject res = WS.sendRequest(req)
			status = res.getStatusCode()  // '200' OK, '302' redirected, '403' etc
		} catch (SendRequestException sre) {
			println("send reques exception occured")
		} catch (SocketException se) {
			println("Socket Exception: Not able to send request to " + url)
		} catch(Exception ex) {
			println('Eception occured')
		}
	}
	return status
}

I have implemented Error Handling try-catch block but still the exception is not caught and I am getting the below error message:

4-07-12 20:02:27.664 ERROR c.k.katalon.core.main.TestCaseExecutor   - ❌ Test Cases/SampleTestCase FAILED.
Reason:
com.kms.katalon.core.exception.StepFailedException: Unable to send request (Root cause: com.kms.katalon.core.webservice.exception.SendRequestException: java.net.SocketException: Connection reset
	at com.kms.katalon.core.webservice.common.HttpUtil.sendRequest(HttpUtil.java:172)
	at com.kms.katalon.core.webservice.common.BasicRequestor.send(BasicRequestor.java:58)
	at com.kms.katalon.core.webservice.helper.WebServiceCommonHelper.sendRequest(WebServiceCommonHelper.java:55)
	at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword$_sendRequest_closure1.doCall(SendRequestKeyword.groovy:40)
	at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword$_sendRequest_closure1.call(SendRequestKeyword.groovy)
	at com.kms.katalon.core.keyword.internal.KeywordMain.runKeyword(KeywordMain.groovy:75)
	at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword.sendRequest(SendRequestKeyword.groovy:38)
	at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword.execute(SendRequestKeyword.groovy:33)
	at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:74)
	at com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords.sendRequest(WSBuiltInKeywords.groovy:41)
	at SampleTestCase.getResponseStatus(SampleTestCase:65)
	at SampleTestCase.run(SampleTestCase:46)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:448)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:439)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:418)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:410)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:137)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:128)
	at TempTestCase1720794736803.run(TempTestCase1720794736803.groovy:25)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Caused by: java.net.SocketException: Connection reset
	at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:436)
	at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:384)
	at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
	at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:376)
	at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)
	at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
	at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
	at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
	at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
	at com.kms.katalon.core.webservice.common.HttpUtil.sendRequest(HttpUtil.java:144)
	at com.kms.katalon.core.webservice.common.BasicRequestor.send(BasicRequestor.java:58)
	at com.kms.katalon.core.webservice.helper.WebServiceCommonHelper.sendRequest(WebServiceCommonHelper.java:55)
	at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword$_sendRequest_closure1.doCall(SendRequestKeyword.groovy:40)
	at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword$_sendRequest_closure1.call(SendRequestKeyword.groovy)
	at com.kms.katalon.core.keyword.internal.KeywordMain.runKeyword(KeywordMain.groovy:75)
	at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword.sendRequest(SendRequestKeyword.groovy:38)
	at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword.execute(SendRequestKeyword.groovy:33)
	at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:74)
	at com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords.sendRequest(WSBuiltInKeywords.groovy:41)
	at Script1718961732884.getResponseStatus(Script1718961732884.groovy:65)
	at Script1718961732884.run(Script1718961732884.groovy:46)
	... 13 more
)

Pls help to resolve the issue.

Hi there,

Thank you very much for your topic. Please note that it may take a little while before a member of our community or from Katalon team responds to you.

Thanks!

Are you trying under vpn?

yes… i’m using a private network and the url for which i’m testing is not accessible but i want to catch that exception/error and want to do some alternate task.

I don’t believe that it is the case. If it is, the Java/Groovy world must have ended many times.

I guess, @hritik has 2 or more Test Cases. @hritik showed us a Test Case with a try {} catch {} clause, but he/she has another one without try{} catch{} and he/she forgot to show it to us. @hritik believes he/she ran a Test Case with try {} catch {} but he/she most probably executed another one without try{} catch{} and got the stackstrace.

I am using this single test case and you can try this as well. The above sample script is running fine for other urls. You can try this as well.
JFYI: I have also imported all the packages and classes.

No, I can’t. Your test case does not disclose the URL. And you mentioned that the URL is a private one. Therefore nobody, other than you, can reproduce your case on their machine.

Hi there @hritik, :wave:

Just checking in to see if you have been able to find the solution to your question within this thread yet.

If yes, then don’t forget to mark helpful replies as a solution :white_check_mark: so that others who may be asking similar questions / running into similar problems can find the solution as well!

Thanks,
Albert

I guess, a keyword threw an exception but the exception did not bubble up to the caller Test Case. The exception was intercepted by someone (a part of Katalon) in the middle. So your Test Case could not see the exception.

Please check Project Settings > Test Design > Test Case

Here you will find an option "Default Failure Handling for Test Step "

  1. STOP_ON_FAILURE
  2. CONTINUE_ONF_FAILURE
  3. OPTIONAL

@hritik,

Which value do you have in your project?

I guess, @hritik has “CONTINUE_ON_FAILURE” or “OPTIONAL” in the his project setup. That is why his try { ... } catch { ... } could catch no exception.

If @hritik changes the option to “STOP_ON_FAILURE”, then his try { ... } catch {...} will be able to catch an exception thrown.

Why an StepFailedException is thrown when STOP_ON_FAILURE is specified, and why no StepFailedException is caught when CONTINUE_ON_FAILURE is specfied?

You need to read the source code of com.kms.katalon.core.keyword.internal.KeywordMain class. It has the following fragment

@CompileStatic
    public static stepFailed(String message, FailureHandling flHandling, Throwable t, Map<String, String> attributes = null) throws StepFailedException {
        String failedMessage = buildReasonMessage(message, t != null ? ExceptionsUtil.getStackTraceForThrowable(t) : EMPTY_REASON).toString()
        switch (flHandling) {
            case FailureHandling.OPTIONAL:
                logger.logWarning(failedMessage, attributes, t);
                break;
            case FailureHandling.CONTINUE_ON_FAILURE:
                logger.logFailed(failedMessage, attributes, t);
                Exception ex = null;
                if (ErrorCollector.isErrorFailed(t)) {
                    ex = new StepErrorException(failedMessage, t)
                }
                ex = new StepFailedException(failedMessage, t)
                ErrorCollector.getCollector().addError(ex);
                break;
            case FailureHandling.STOP_ON_FAILURE:
                logger.logFailed(failedMessage, attributes, t);
                ErrorCollector.getCollector().setStopLauncher(true);
                if (t instanceof StepFailedException || t instanceof StepErrorException) {
                    throw t;
                }
                if (ErrorCollector.isErrorFailed(t)) {
                    throw new StepErrorException(failedMessage, t)
                }
                throw new StepFailedException(failedMessage, t);
        }
    }

When a keyword failed, the stepFailed() method of KeywordMain will be called. This method manages how the failure to be handled.

When STOP_ON_FAILURE is given, an Exception will be re-thrown, which will bubble up to the caller Test Case.

When CONTINUE_ON_FAILURE is given, any Exception from keyword classes will be just recorded at the line

             ErrorCollector.getCollector().addError(ex);

but the exception will not be re-thrown.

When a Test Case finished, Katalon Studio will pop the exception out of the stack in ErrorCollector and print statck trace messages into the console. You may misunderstand that the stacktrace is printed as soon as a keyword threw it. No, the exception had been stacked once, and later post-processed. Therefore a try {...} catch {...} in your Test Case script will never see the exception.

Now it is clear why @hritik could not see any Exceptions from keywords. I guess, he has CONTINUE_ON_FAILURE or OPTIONAL as default behavior, so no Excepiton from keyword failures will bubble up to Test Case.

How could I get the source code of com.kms.katalon.core.keyword.internal.KeywordMain class?

On my Mac, I visited

/Applications/Katalon Studio.app/Contents/Eclipse/configuration/resources/source

where I could find jar files which contains the source . On windows, you should be able to find the source as well in the Katalon installation folder.