Skip to content

Commit

Permalink
Merge pull request #1407 from dilanSachi/fix-error-name
Browse files Browse the repository at this point in the history
Fix error constant name
  • Loading branch information
dilanSachi authored Oct 2, 2023
2 parents 59da879 + db81f82 commit e816fe8
Show file tree
Hide file tree
Showing 9 changed files with 424 additions and 7 deletions.
24 changes: 24 additions & 0 deletions ballerina-tests/tests/78_server_error_types.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2023 WSO2 LLC. (http://www.wso2.com) All Rights Reserved.
//
// WSO2 LLC. 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.

syntax = "proto3";

import "google/protobuf/empty.proto";
import "google/protobuf/wrappers.proto";

service ServerErrorTypesService {
rpc GetServerError(google.protobuf.Int32Value) returns (google.protobuf.Empty);
}
225 changes: 225 additions & 0 deletions ballerina-tests/tests/78_server_error_types_client.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
// Copyright (c) 2023 WSO2 LLC. (http://www.wso2.com) All Rights Reserved.
//
// WSO2 LLC. 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.

import ballerina/io;
import ballerina/grpc;
import ballerina/test;

@test:Config {enable: true}
function testErrorTypesCancelledError() returns error? {
ServerErrorTypesServiceClient errorClient = check new ("http://localhost:9178");
error? response = errorClient->GetServerError(1);
io:println(response);
if response is grpc:CancelledError {
test:assertEquals(response.message(), "Cancelled execution");
} else {
test:assertFail("Expected error type is not returned");
}
}

@test:Config {enable: true}
function testErrorTypesUnKnownError() returns error? {
ServerErrorTypesServiceClient errorClient = check new ("http://localhost:9178");
error? response = errorClient->GetServerError(2);
io:println(response);
if response is grpc:UnKnownError {
test:assertEquals(response.message(), "Unknown request");
} else {
test:assertFail("Expected error type is not returned");
}
}

@test:Config {enable: true}
function testErrorTypesInvalidArgumentError() returns error? {
ServerErrorTypesServiceClient errorClient = check new ("http://localhost:9178");
error? response = errorClient->GetServerError(3);
io:println(response);
if response is grpc:InvalidArgumentError {
test:assertEquals(response.message(), "Invalid argument");
} else {
test:assertFail("Expected error type is not returned");
}
}

@test:Config {enable: true}
function testErrorTypesDeadlineExceededError() returns error? {
ServerErrorTypesServiceClient errorClient = check new ("http://localhost:9178");
error? response = errorClient->GetServerError(4);
io:println(response);
if response is grpc:DeadlineExceededError {
test:assertEquals(response.message(), "Deadline exceeded");
} else {
test:assertFail("Expected error type is not returned");
}
}

@test:Config {enable: true}
function testErrorTypesNotFoundError() returns error? {
ServerErrorTypesServiceClient errorClient = check new ("http://localhost:9178");
error? response = errorClient->GetServerError(5);
io:println(response);
if response is grpc:NotFoundError {
test:assertEquals(response.message(), "Not found");
} else {
test:assertFail("Expected error type is not returned");
}
}

@test:Config {enable: true}
function testErrorTypesAlreadyExistsError() returns error? {
ServerErrorTypesServiceClient errorClient = check new ("http://localhost:9178");
error? response = errorClient->GetServerError(6);
io:println(response);
if response is grpc:AlreadyExistsError {
test:assertEquals(response.message(), "Already exists");
} else {
test:assertFail("Expected error type is not returned");
}
}

@test:Config {enable: true}
function testErrorTypesPermissionDeniedError() returns error? {
ServerErrorTypesServiceClient errorClient = check new ("http://localhost:9178");
error? response = errorClient->GetServerError(7);
io:println(response);
if response is grpc:PermissionDeniedError {
test:assertEquals(response.message(), "Permission denied");
} else {
test:assertFail("Expected error type is not returned");
}
}

@test:Config {enable: true}
function testErrorTypesUnauthenticatedError() returns error? {
ServerErrorTypesServiceClient errorClient = check new ("http://localhost:9178");
error? response = errorClient->GetServerError(8);
io:println(response);
if response is grpc:UnauthenticatedError {
test:assertEquals(response.message(), "Unauthenticated");
} else {
test:assertFail("Expected error type is not returned");
}
}

@test:Config {enable: true}
function testErrorTypesResourceExhaustedError() returns error? {
ServerErrorTypesServiceClient errorClient = check new ("http://localhost:9178");
error? response = errorClient->GetServerError(9);
io:println(response);
if response is grpc:ResourceExhaustedError {
test:assertEquals(response.message(), "Resource exhausted");
} else {
test:assertFail("Expected error type is not returned");
}
}

@test:Config {enable: true}
function testErrorTypesFailedPreconditionError() returns error? {
ServerErrorTypesServiceClient errorClient = check new ("http://localhost:9178");
error? response = errorClient->GetServerError(10);
io:println(response);
if response is grpc:FailedPreconditionError {
test:assertEquals(response.message(), "Failed precondition");
} else {
test:assertFail("Expected error type is not returned");
}
}

@test:Config {enable: true}
function testErrorTypesAbortedError() returns error? {
ServerErrorTypesServiceClient errorClient = check new ("http://localhost:9178");
error? response = errorClient->GetServerError(11);
io:println(response);
if response is grpc:AbortedError {
test:assertEquals(response.message(), "Aborted execution");
} else {
test:assertFail("Expected error type is not returned");
}
}

@test:Config {enable: true}
function testErrorTypesOutOfRangeError() returns error? {
ServerErrorTypesServiceClient errorClient = check new ("http://localhost:9178");
error? response = errorClient->GetServerError(12);
io:println(response);
if response is grpc:OutOfRangeError {
test:assertEquals(response.message(), "Out of range");
} else {
test:assertFail("Expected error type is not returned");
}
}

@test:Config {enable: true}
function testErrorTypesUnimplementedError() returns error? {
ServerErrorTypesServiceClient errorClient = check new ("http://localhost:9178");
error? response = errorClient->GetServerError(13);
io:println(response);
if response is grpc:UnimplementedError {
test:assertEquals(response.message(), "Unimplemented");
} else {
test:assertFail("Expected error type is not returned");
}
}

@test:Config {enable: true}
function testErrorTypesInternalError() returns error? {
ServerErrorTypesServiceClient errorClient = check new ("http://localhost:9178");
error? response = errorClient->GetServerError(14);
io:println(response);
if response is grpc:InternalError {
test:assertEquals(response.message(), "Internal error");
} else {
test:assertFail("Expected error type is not returned");
}
}

@test:Config {enable: true}
function testErrorTypesUnavailableError() returns error? {
ServerErrorTypesServiceClient errorClient = check new ("http://localhost:9178");
error? response = errorClient->GetServerError(15);
io:println(response);
if response is grpc:UnavailableError {
test:assertEquals(response.message(), "Unavailable");
} else {
test:assertFail("Expected error type is not returned");
}
}

@test:Config {enable: true}
function testErrorTypesDataLossError() returns error? {
ServerErrorTypesServiceClient errorClient = check new ("http://localhost:9178");
error? response = errorClient->GetServerError(16);
io:println(response);
if response is grpc:DataLossError {
test:assertEquals(response.message(), "Data loss");
} else {
test:assertFail("Expected error type is not returned");
}
}

@test:Config {enable: true}
function testErrorTypesUnknownErrorType() returns error? {
ServerErrorTypesServiceClient errorClient = check new ("http://localhost:9178");
error? response = errorClient->GetServerError(22);
io:println(response);
if response is error {
test:assertEquals(response.message(), "Unknown error type");
} else {
test:assertFail("Expected error type is not returned");
}
}


83 changes: 83 additions & 0 deletions ballerina-tests/tests/78_server_error_types_pb.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) 2023 WSO2 LLC. (http://www.wso2.com) All Rights Reserved.
//
// WSO2 LLC. 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.

import ballerina/grpc;
import ballerina/protobuf.types.empty;
import ballerina/protobuf.types.wrappers;

public const string SERVER_ERROR_TYPES_DESC = "0A1B37385F7365727665725F6572726F725F74797065732E70726F746F1A1B676F6F676C652F70726F746F6275662F656D7074792E70726F746F1A1E676F6F676C652F70726F746F6275662F77726170706572732E70726F746F32600A175365727665724572726F7254797065735365727669636512450A0E4765745365727665724572726F72121B2E676F6F676C652E70726F746F6275662E496E74333256616C75651A162E676F6F676C652E70726F746F6275662E456D707479620670726F746F33";

public isolated client class ServerErrorTypesServiceClient {
*grpc:AbstractClientEndpoint;

private final grpc:Client grpcClient;

public isolated function init(string url, *grpc:ClientConfiguration config) returns grpc:Error? {
self.grpcClient = check new (url, config);
check self.grpcClient.initStub(self, SERVER_ERROR_TYPES_DESC);
}

isolated remote function GetServerError(int|wrappers:ContextInt req) returns grpc:Error? {
map<string|string[]> headers = {};
int message;
if req is wrappers:ContextInt {
message = req.content;
headers = req.headers;
} else {
message = req;
}
_ = check self.grpcClient->executeSimpleRPC("ServerErrorTypesService/GetServerError", message, headers);
}

isolated remote function GetServerErrorContext(int|wrappers:ContextInt req) returns empty:ContextNil|grpc:Error {
map<string|string[]> headers = {};
int message;
if req is wrappers:ContextInt {
message = req.content;
headers = req.headers;
} else {
message = req;
}
var payload = check self.grpcClient->executeSimpleRPC("ServerErrorTypesService/GetServerError", message, headers);
[anydata, map<string|string[]>] [_, respHeaders] = payload;
return {headers: respHeaders};
}
}

public client class ServerErrorTypesServiceNilCaller {
private grpc:Caller caller;

public isolated function init(grpc:Caller caller) {
self.caller = caller;
}

public isolated function getId() returns int {
return self.caller.getId();
}

isolated remote function sendError(grpc:Error response) returns grpc:Error? {
return self.caller->sendError(response);
}

isolated remote function complete() returns grpc:Error? {
return self.caller->complete();
}

public isolated function isCancelled() returns boolean {
return self.caller.isCancelled();
}
}

Loading

0 comments on commit e816fe8

Please sign in to comment.