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

Log error when the return from the remote method leads to an error #200

Merged
merged 12 commits into from
Jun 15, 2021
7 changes: 5 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Multipart content delivery using websubhub-client.
- Subscription Lease seconds expired event.

### Fixed

- [Log error when return from the remote method leads to an error](https://github.com/ballerina-platform/ballerina-standard-library/issues/1449)
- [WebSubHub Compiler Plugin does not allow additional methods inside service declaration](https://github.com/ballerina-platform/ballerina-standard-library/issues/1417)

## [0.2.0-beta.1] - 2021-05-06

Expand Down
2 changes: 1 addition & 1 deletion websubhub-ballerina/commons.bal
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ isolated function isSuccessStatusCode(int statusCode) returns boolean {
# + topic - Name of the `topic`
# + return - a `string` containing the value for `HTTP Link Header`
isolated function generateLinkUrl(string hubUrl, string topic) returns string {
return string`${hubUrl}; rel=\"hub\", ${topic}; rel=\"self\"`;
return string `${hubUrl}; rel=\"hub\", ${topic}; rel=\"self\"`;
}

# Converts {@code websubhub:ClientConfiguration} to {@code http:ClientConfiguration}
Expand Down
2 changes: 1 addition & 1 deletion websubhub-ballerina/hub_client.bal
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public client class HubClient {
} else {
hash = check retrievePayloadSignature(self.secret, msg.content);
}
request.setHeader(X_HUB_SIGNATURE, string`${SHA256_HMAC}=${hash.toBase16()}`);
request.setHeader(X_HUB_SIGNATURE, string `${SHA256_HMAC}=${hash.toBase16()}`);
}

http:Response|error response = self.httpClient->post("/", request);
Expand Down
40 changes: 16 additions & 24 deletions websubhub-ballerina/tests/hub_with_error_return_types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,40 @@ listener Listener hubWithErrorReturnTypesListener = new(9101);

service /websubhub on hubWithErrorReturnTypesListener {

isolated remote function onRegisterTopic(TopicRegistration message)
returns TopicRegistrationSuccess|TopicRegistrationError|error {
return error Error("Registration Failed!");
isolated remote function onRegisterTopic(TopicRegistration message) returns error {
return error ("Registration Failed!");
}

isolated remote function onDeregisterTopic(TopicDeregistration message)
returns TopicDeregistrationSuccess|TopicDeregistrationError|error {
return error Error("Topic Deregistration Failed!");
isolated remote function onDeregisterTopic(TopicDeregistration message) returns error {
return error ("Topic Deregistration Failed!");
}

isolated remote function onUpdateMessage(UpdateMessage msg)
returns Acknowledgement|UpdateMessageError|error {
return error UpdateMessageError("Error in accessing content");
isolated remote function onUpdateMessage(UpdateMessage msg) returns error {
return error ("Error in accessing content");
}

isolated remote function onSubscription(Subscription msg)
returns SubscriptionAccepted|SubscriptionPermanentRedirect|SubscriptionTemporaryRedirect
|BadSubscriptionError|InternalSubscriptionError|error {
return error Error("Error occurred while processing subscription");
isolated remote function onSubscription(Subscription msg) returns error {
return error ("Error occurred while processing subscription");
}

isolated remote function onSubscriptionValidation(Subscription msg)
returns SubscriptionDeniedError|error? {
return error Error("Denied subscription with Hub");
isolated remote function onSubscriptionValidation(Subscription msg) returns error? {
return error ("Denied subscription with Hub");
}

isolated remote function onSubscriptionIntentVerified(VerifiedSubscription msg) returns error? {
return error Error("Error occcurred while verifying subscription intent");
return error ("Error occcurred while verifying subscription intent");
}

isolated remote function onUnsubscription(Unsubscription msg)
returns UnsubscriptionAccepted|BadUnsubscriptionError|InternalUnsubscriptionError|error {
return error Error("Denied unsubscription for topic '" + <string> msg.hubTopic + "'");
isolated remote function onUnsubscription(Unsubscription msg) returns error {
return error ("Denied unsubscription for topic '" + <string> msg.hubTopic + "'");
}

isolated remote function onUnsubscriptionValidation(Unsubscription msg)
returns UnsubscriptionDeniedError|error? {
return error UnsubscriptionDeniedError("Denied subscription with Hub");
isolated remote function onUnsubscriptionValidation(Unsubscription msg) returns error? {
return error ("Denied subscription with Hub");
}

isolated remote function onUnsubscriptionIntentVerified(VerifiedUnsubscription msg) returns error? {
return error Error("Error occcurred while verifying unsubscription intent");
return error ("Error occcurred while verifying unsubscription intent");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ public configurable int MESSAGE_DELIVERY_COUNT = 3;
# The message delivery timeout
public configurable decimal MESSAGE_DELIVERY_TIMEOUT = 10;

public final string CONSTRUCTED_SERVER_ID = string`${SERVER_ID}-${util:generateRandomString()}`;
public final string CONSTRUCTED_SERVER_ID = string `${SERVER_ID}-${util:generateRandomString()}`;
2 changes: 1 addition & 1 deletion websubhub-native/spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<FindBugsFilter>
<Match>
<OR>
<Package name="org.ballerinalang.net.websub.hub*"/>
<Package name="io.ballerina.stdlib.websubhub*"/>
</OR>
<OR>
<Bug pattern="IS2_INCONSISTENT_SYNC"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.ballerina.stdlib.websubhub;

/**
* {@code Constants} contains the public constants to be used.
*/
public interface Constants {
String PACKAGE_ORG = "ballerina";
String PACKAGE_NAME = "websubhub";

String SERVICE_OBJECT = "WEBSUBHUB_SERVICE_OBJECT";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.ballerina.stdlib.websubhub;

import io.ballerina.runtime.api.Future;
import io.ballerina.runtime.api.Module;
import io.ballerina.runtime.api.async.Callback;
import io.ballerina.runtime.api.creators.ErrorCreator;
import io.ballerina.runtime.api.types.Type;
import io.ballerina.runtime.api.values.BError;
import io.ballerina.runtime.api.values.BString;

import static io.ballerina.runtime.api.utils.StringUtils.fromString;

/**
* {@code HubCallback} used to handle the websubhub remote method invocation results.
*/
public class HubCallback implements Callback {
private final Future future;
private final Module module;

public HubCallback(Future future, Module module) {
this.future = future;
this.module = module;
}

@Override
public void notifySuccess(Object result) {
if (result instanceof BError) {
BError error = (BError) result;
if (!isModuleDefinedError(error)) {
error.printStackTrace();
}
}

future.complete(result);
}

@Override
public void notifyFailure(BError bError) {
bError.printStackTrace();
BString errorMessage = fromString("service method invocation failed: " + bError.getErrorMessage());
BError invocationError = ErrorCreator.createError(module, "ServiceExecutionError",
errorMessage, bError, null);
future.complete(invocationError);
}

private boolean isModuleDefinedError(BError error) {
Type errorType = error.getType();
Module packageDetails = errorType.getPackage();
String orgName = packageDetails.getOrg();
String packageName = packageDetails.getName();
return Constants.PACKAGE_ORG.equals(orgName) && Constants.PACKAGE_NAME.equals(packageName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,23 @@
import io.ballerina.runtime.api.Environment;
import io.ballerina.runtime.api.Future;
import io.ballerina.runtime.api.Module;
import io.ballerina.runtime.api.async.Callback;
import io.ballerina.runtime.api.async.StrandMetadata;
import io.ballerina.runtime.api.creators.ErrorCreator;
import io.ballerina.runtime.api.creators.ValueCreator;
import io.ballerina.runtime.api.types.MethodType;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.values.BArray;
import io.ballerina.runtime.api.values.BError;
import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BObject;
import io.ballerina.runtime.api.values.BString;

import java.util.ArrayList;

import static io.ballerina.runtime.api.utils.StringUtils.fromString;
import static io.ballerina.stdlib.websubhub.Constants.SERVICE_OBJECT;

/**
* {@code NativeHttpToWebsubhubAdaptor} is a wrapper object used for service method execution.
*/
public class NativeHttpToWebsubhubAdaptor {
private static final String SERVICE_OBJECT = "WEBSUBHUB_SERVICE_OBJECT";

public static void externInit(BObject adaptor, BObject serviceObj) {
adaptor.addNativeData(SERVICE_OBJECT, serviceObj);
}
Expand Down Expand Up @@ -135,20 +130,8 @@ private static Object invokeRemoteFunction(Environment env, BObject bHubService,
Module module = ModuleUtils.getModule();
StrandMetadata metadata = new StrandMetadata(module.getOrg(), module.getName(), module.getVersion(),
parentFunctionName);
env.getRuntime().invokeMethodAsync(bHubService, remoteFunctionName, null, metadata, new Callback() {
@Override
public void notifySuccess(Object result) {
balFuture.complete(result);
}

@Override
public void notifyFailure(BError bError) {
BString errorMessage = fromString("service method invocation failed: " + bError.getErrorMessage());
BError invocationError = ErrorCreator.createError(module, "ServiceExecutionError",
errorMessage, bError, null);
balFuture.complete(invocationError);
}
}, args);
env.getRuntime().invokeMethodAsync(bHubService, remoteFunctionName, null, metadata,
new HubCallback(balFuture, module), args);
return null;
}
}