diff --git a/native/src/main/java/io/ballerina/stdlib/http/api/BallerinaHTTPConnectorListener.java b/native/src/main/java/io/ballerina/stdlib/http/api/BallerinaHTTPConnectorListener.java index f9860fb8aa..0bdac2a974 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/api/BallerinaHTTPConnectorListener.java +++ b/native/src/main/java/io/ballerina/stdlib/http/api/BallerinaHTTPConnectorListener.java @@ -87,7 +87,7 @@ public void onMessage(HttpCarbonMessage inboundMessage) { } catch (Exception ex) { HttpRequestInterceptorUnitCallback callback = new HttpRequestInterceptorUnitCallback(inboundMessage, httpServicesRegistry.getRuntime(), this); - callback.invokeErrorInterceptors(HttpUtil.createError(ex), false); + callback.invokeErrorInterceptors(HttpUtil.createError(ex), true); return; } @@ -101,7 +101,7 @@ public void onMessage(HttpCarbonMessage inboundMessage) { } catch (Exception ex) { HttpCallableUnitCallback callback = new HttpCallableUnitCallback(inboundMessage, httpServicesRegistry.getRuntime()); - callback.invokeErrorInterceptors(HttpUtil.createError(ex), false); + callback.invokeErrorInterceptors(HttpUtil.createError(ex), true); } } } @@ -285,7 +285,7 @@ protected void executeMainResourceOnMessage(HttpCarbonMessage inboundMessage) { } catch (BallerinaConnectorException ex) { HttpCallableUnitCallback callback = new HttpCallableUnitCallback(inboundMessage, httpServicesRegistry.getRuntime()); - callback.invokeErrorInterceptors(HttpUtil.createError(ex), false); + callback.invokeErrorInterceptors(HttpUtil.createError(ex), true); } } @@ -319,7 +319,7 @@ private void setTargetServiceToInboundMsg(HttpCarbonMessage inboundMessage) { } catch (Exception e) { if (((BArray) listenerLevelInterceptors).size() == 1 && e instanceof BError && ((BError) e).getType().getName() - .equals(HttpErrorType.SERVICE_NOT_FOUND_ERROR.getErrorName())) { + .equals(HttpErrorType.INTERNAL_SERVICE_NOT_FOUND_ERROR.getErrorName())) { HttpService singleService = HttpDispatcher.findSingleService(httpServicesRegistry); if (singleService != null && singleService.hasInterceptors()) { inboundMessage.setProperty(INTERCEPTORS, singleService.getBalInterceptorServicesArray()); diff --git a/native/src/main/java/io/ballerina/stdlib/http/api/HttpCallableUnitCallback.java b/native/src/main/java/io/ballerina/stdlib/http/api/HttpCallableUnitCallback.java index 09a58b2be9..3eedc79796 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/api/HttpCallableUnitCallback.java +++ b/native/src/main/java/io/ballerina/stdlib/http/api/HttpCallableUnitCallback.java @@ -92,7 +92,7 @@ public void notifySuccess(Object result) { return; } if (result instanceof BError) { - invokeErrorInterceptors((BError) result, true); + invokeErrorInterceptors((BError) result, false); return; } if (isLastService) { @@ -130,7 +130,6 @@ public void invokeBalMethod(Object[] paramFeed, String methodName) { @Override public void notifySuccess(Object result) { stopObserverContext(); - printStacktraceIfError(result); } @Override @@ -166,17 +165,19 @@ public void notifyFailure(BError error) { // handles panic and check_panic System.exit(1); } - public void invokeErrorInterceptors(BError error, boolean printError) { - requestMessage.setProperty(HttpConstants.INTERCEPTOR_SERVICE_ERROR, error); - if (printError) { - error.printStackTrace(); + public void invokeErrorInterceptors(BError error, boolean isInternalError) { + if (isInternalError) { + requestMessage.setProperty(HttpConstants.INTERNAL_ERROR, true); + } else { + requestMessage.removeProperty(HttpConstants.INTERNAL_ERROR); } + requestMessage.setProperty(HttpConstants.INTERCEPTOR_SERVICE_ERROR, error); returnErrorResponse(error); } public void sendFailureResponse(BError error) { stopObserverContext(); - HttpUtil.handleFailure(requestMessage, error, true); + HttpUtil.handleFailure(requestMessage, error); } public void cleanupRequestMessage() { @@ -186,19 +187,13 @@ public void cleanupRequestMessage() { private boolean alreadyResponded(Object result) { try { HttpUtil.methodInvocationCheck(requestMessage, HttpConstants.INVALID_STATUS_CODE, ILLEGAL_FUNCTION_INVOKED); - } catch (BError e) { + } catch (BError bError) { if (result != null) { // handles nil return and end of resource exec - printStacktraceIfError(result); - err.println(HttpConstants.HTTP_RUNTIME_WARNING_PREFIX + e.getMessage()); + bError.printStackTrace(); + err.println(HttpConstants.HTTP_RUNTIME_WARNING_PREFIX + bError.getMessage()); } return true; } return false; } - - private void printStacktraceIfError(Object result) { - if (result instanceof BError) { - ((BError) result).printStackTrace(); - } - } } diff --git a/native/src/main/java/io/ballerina/stdlib/http/api/HttpConstants.java b/native/src/main/java/io/ballerina/stdlib/http/api/HttpConstants.java index 286e621115..f1d8af5557 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/api/HttpConstants.java +++ b/native/src/main/java/io/ballerina/stdlib/http/api/HttpConstants.java @@ -382,6 +382,7 @@ public final class HttpConstants { public static final String REQUEST_INTERCEPTOR_INDEX = "REQUEST_INTERCEPTOR_INDEX"; public static final String RESPONSE_INTERCEPTOR_INDEX = "RESPONSE_INTERCEPTOR_INDEX"; public static final String INTERCEPTOR_SERVICE_ERROR = "INTERCEPTOR_SERVICE_ERROR"; + public static final String INTERNAL_ERROR = "INTERNAL_ERROR"; public static final String WAIT_FOR_FULL_REQUEST = "WAIT_FOR_FULL_REQUEST"; public static final String HTTP_NORMAL = "Normal"; public static final String REQUEST_INTERCEPTOR = "RequestInterceptor"; diff --git a/native/src/main/java/io/ballerina/stdlib/http/api/HttpRequestInterceptorUnitCallback.java b/native/src/main/java/io/ballerina/stdlib/http/api/HttpRequestInterceptorUnitCallback.java index 76cc1302c3..dc10dc7a43 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/api/HttpRequestInterceptorUnitCallback.java +++ b/native/src/main/java/io/ballerina/stdlib/http/api/HttpRequestInterceptorUnitCallback.java @@ -29,7 +29,7 @@ import io.ballerina.stdlib.http.api.nativeimpl.ModuleUtils; import io.ballerina.stdlib.http.transport.message.HttpCarbonMessage; -import static io.ballerina.stdlib.http.api.HttpErrorType.INTERCEPTOR_RETURN_ERROR; +import static io.ballerina.stdlib.http.api.HttpErrorType.INTERNAL_INTERCEPTOR_RETURN_ERROR; /** * {@code HttpRequestInterceptorUnitCallback} is the responsible for acting on notifications received from Ballerina @@ -61,7 +61,7 @@ public void notifySuccess(Object result) { if (!result.equals(requestCtx.getNativeData(HttpConstants.TARGET_SERVICE))) { requestMessage.setHttpStatusCode(500); } - invokeErrorInterceptors((BError) result, true); + invokeErrorInterceptors((BError) result, false); return; } validateResponseAndProceed(result); @@ -74,11 +74,13 @@ public void notifyFailure(BError error) { // handles panic and check_panic System.exit(1); } - public void invokeErrorInterceptors(BError error, boolean printError) { - requestMessage.setProperty(HttpConstants.INTERCEPTOR_SERVICE_ERROR, error); - if (printError) { - error.printStackTrace(); + public void invokeErrorInterceptors(BError error, boolean isInternalError) { + if (isInternalError) { + requestMessage.setProperty(HttpConstants.INTERNAL_ERROR, true); + } else { + requestMessage.removeProperty(HttpConstants.INTERNAL_ERROR); } + requestMessage.setProperty(HttpConstants.INTERCEPTOR_SERVICE_ERROR, error); ballerinaHTTPConnectorListener.onMessage(requestMessage); } @@ -101,12 +103,6 @@ private boolean alreadyResponded() { return false; } - private void printStacktraceIfError(Object result) { - if (result instanceof BError) { - ((BError) result).printStackTrace(); - } - } - private void sendRequestToNextService() { ballerinaHTTPConnectorListener.onMessage(requestMessage); } @@ -152,7 +148,7 @@ private void validateServiceReturnType(Object result, int interceptorId, BArray sendRequestToNextService(); } else { String message = "next interceptor service did not match with the configuration"; - BError err = HttpUtil.createHttpStatusCodeError(INTERCEPTOR_RETURN_ERROR, message); + BError err = HttpUtil.createHttpStatusCodeError(INTERNAL_INTERCEPTOR_RETURN_ERROR, message); invokeErrorInterceptors(err, true); } } else { @@ -161,7 +157,7 @@ private void validateServiceReturnType(Object result, int interceptorId, BArray sendRequestToNextService(); } else { String message = "target service did not match with the configuration"; - BError err = HttpUtil.createHttpStatusCodeError(INTERCEPTOR_RETURN_ERROR, message); + BError err = HttpUtil.createHttpStatusCodeError(INTERNAL_INTERCEPTOR_RETURN_ERROR, message); invokeErrorInterceptors(err, true); } } @@ -186,13 +182,12 @@ public void invokeBalMethod(Object[] paramFeed, String methodName) { Callback returnCallback = new Callback() { @Override public void notifySuccess(Object result) { - printStacktraceIfError(result); } @Override public void notifyFailure(BError result) { cleanupRequestMessage(); - HttpUtil.handleFailure(requestMessage, result, false); + HttpUtil.handleFailure(requestMessage, result); } }; runtime.invokeMethodAsyncSequentially( diff --git a/native/src/main/java/io/ballerina/stdlib/http/api/HttpResponseInterceptorUnitCallback.java b/native/src/main/java/io/ballerina/stdlib/http/api/HttpResponseInterceptorUnitCallback.java index 00f5d223c2..f9e8d76fff 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/api/HttpResponseInterceptorUnitCallback.java +++ b/native/src/main/java/io/ballerina/stdlib/http/api/HttpResponseInterceptorUnitCallback.java @@ -64,7 +64,7 @@ public HttpResponseInterceptorUnitCallback(HttpCarbonMessage requestMessage, BOb public void notifySuccess(Object result) { if (result instanceof BError) { requestMessage.setHttpStatusCode(500); - invokeErrorInterceptors((BError) result, true); + invokeErrorInterceptors((BError) result, false); return; } if (possibleLastInterceptor) { @@ -80,20 +80,16 @@ public void notifyFailure(BError error) { // handles panic and check_panic System.exit(1); } - public void invokeErrorInterceptors(BError error, boolean printError) { - requestMessage.setProperty(HttpConstants.INTERCEPTOR_SERVICE_ERROR, error); - if (printError) { - error.printStackTrace(); + public void invokeErrorInterceptors(BError error, boolean isInternalError) { + if (isInternalError) { + requestMessage.setProperty(HttpConstants.INTERNAL_ERROR, true); + } else { + requestMessage.removeProperty(HttpConstants.INTERNAL_ERROR); } + requestMessage.setProperty(HttpConstants.INTERCEPTOR_SERVICE_ERROR, error); returnErrorResponse(error); } - private void printStacktraceIfError(Object result) { - if (result instanceof BError) { - ((BError) result).printStackTrace(); - } - } - private void sendResponseToNextService() { Respond.nativeRespondWithDataCtx(environment, caller, response, dataContext); } @@ -142,8 +138,8 @@ private void validateServiceReturnType(Object result, int interceptorId, BArray if (result.equals(interceptor)) { sendResponseToNextService(); } else { - String message = "next interceptor service did not match with the configuration"; - BError err = HttpUtil.createHttpStatusCodeError(HttpErrorType.INTERCEPTOR_RETURN_ERROR, message); + BError err = HttpUtil.createHttpStatusCodeError(HttpErrorType.INTERNAL_INTERCEPTOR_RETURN_ERROR, + "next interceptor service did not match with the configuration"); invokeErrorInterceptors(err, true); } } @@ -171,7 +167,6 @@ public void invokeBalMethod(Object[] paramFeed, String methodName) { public void notifySuccess(Object result) { stopObserverContext(); dataContext.notifyOutboundResponseStatus(null); - printStacktraceIfError(result); } @Override diff --git a/native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java b/native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java index 6d8b96e59e..02d57796a4 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java +++ b/native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java @@ -493,12 +493,10 @@ public static void handleFailure(HttpCarbonMessage requestMessage, String errorM PipeliningHandler.sendPipelinedResponse(requestMessage, createErrorMessage(errorMsg, statusCode)); } - public static void handleFailure(HttpCarbonMessage requestMessage, BError error, Boolean printStackTrace) { + public static void handleFailure(HttpCarbonMessage requestMessage, BError error) { String errorMsg = getErrorMessage(error); int statusCode = getStatusCode(requestMessage, errorMsg); - if (printStackTrace) { - error.printStackTrace(); - } + error.printStackTrace(); PipeliningHandler.sendPipelinedResponse(requestMessage, createErrorMessage(errorMsg, statusCode)); }