Skip to content

Commit

Permalink
Remove error logging from callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK committed Jan 29, 2024
1 parent 254b865 commit 6b454b1
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -130,7 +130,6 @@ public void invokeBalMethod(Object[] paramFeed, String methodName) {
@Override
public void notifySuccess(Object result) {
stopObserverContext();
printStacktraceIfError(result);
}

@Override
Expand Down Expand Up @@ -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() {
Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
}
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -171,7 +167,6 @@ public void invokeBalMethod(Object[] paramFeed, String methodName) {
public void notifySuccess(Object result) {
stopObserverContext();
dataContext.notifyOutboundResponseStatus(null);
printStacktraceIfError(result);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down

0 comments on commit 6b454b1

Please sign in to comment.