Skip to content

Commit

Permalink
Introduce module specific errors
Browse files Browse the repository at this point in the history
  • Loading branch information
niveathika committed Nov 15, 2023
1 parent abe88ec commit 8941807
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 12 deletions.
42 changes: 32 additions & 10 deletions ballerina/data_mappings.bal
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ isolated function convertOASMessageToMessage(oas:Message response) returns Messa

string? rawMessage = response.raw;
if rawMessage is string {
email.raw = check base64UrlDecode(rawMessage);
string|error decodedMessage = base64UrlDecode(rawMessage);
if decodedMessage is error {
return error InvalidEncodedValue(
string `Returned message raw field${decodedMessage.message()}`, decodedMessage.cause());
}
email.raw = decodedMessage;
}

email.labelIds = response.labelIds ?: email.labelIds;
Expand Down Expand Up @@ -102,7 +107,13 @@ returns MessagePart|error {
messagePart.attachmentId = body.attachmentId ?: messagePart.attachmentId;
messagePart.size = body.size ?: messagePart.size;
if body.data is string {
messagePart.data = check base64UrlDecode(body.data ?: EMPTY_STRING);
string|error decodedBody = base64UrlDecode(body.data ?: EMPTY_STRING);
if decodedBody is error {
return error InvalidEncodedValue(
string `Returned message body part of id '${messagePart.partId}'${decodedBody.message()}`,
decodedBody.cause());
}
messagePart.data = decodedBody;
}
}

Expand Down Expand Up @@ -234,17 +245,22 @@ isolated function getFileMessageString(AttachmentFile|ImageFile file, string emb
if file is ImageFile {
fileString += CONTENT_ID + COLON + file.contentId + NEW_LINE;
}
fileString += NEW_LINE + check getEncodedFileContent(file.path) + NEW_LINE;
fileString += NEW_LINE + check getEncodedFileContent(file.path, embedType) + NEW_LINE;
return fileString;
}

// todo check error usages
isolated function getEncodedFileContent(string filePath) returns string|error {
io:ReadableByteChannel fileChannel = check io:openReadableFile(filePath);
io:ReadableByteChannel fileContent = check fileChannel.base64Encode();
io:ReadableByteChannel encodedFileChannel = fileContent;
byte[] readChannel = check encodedFileChannel.read(100000000);
return string:fromBytes(readChannel);
isolated function getEncodedFileContent(string filePath, string embedType) returns string|error {
do {
io:ReadableByteChannel fileChannel = check io:openReadableFile(filePath);
io:ReadableByteChannel fileContent = check fileChannel.base64Encode();
io:ReadableByteChannel encodedFileChannel = fileContent;
byte[] readChannel = check encodedFileChannel.read(100000000);
return string:fromBytes(readChannel);
} on fail error e {
return error FileGenericError(
string `Unable to retrieve ${embedType}: ${filePath}`, e);
}
}

isolated function convertOASListMessagesResponseToListMessageResponse(oas:ListMessagesResponse response)
Expand Down Expand Up @@ -274,7 +290,13 @@ isolated function convertOASMessagePartBodyToAttachment(oas:MessagePartBody body
attachment.size = bodyPart.size ?: attachment.size;
string? data = bodyPart.data;
if data is string {
attachment.data = check base64UrlDecode(data);
string|error decodedData = base64UrlDecode(data);
if decodedData is error {
return error InvalidEncodedValue(
string `Returned attachment message body part of id '${attachment.attachmentId ?: EMPTY_STRING}'
${decodedData.message()}`, decodedData.cause());
}
attachment.data = decodedData;
}
return attachment;
}
Expand Down
25 changes: 25 additions & 0 deletions ballerina/errors.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2023, 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.

# Defines the generic error type for the `gmail` module.
public type Error distinct error;

# Represents a generic error for the file path.
public type FileGenericError distinct Error;

# Represents a generic error for the file path.
public type InvalidEncodedValue distinct Error;

38 changes: 38 additions & 0 deletions ballerina/tests/test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,41 @@ function testReplyTo() returns error? {
}
check gmailClient->/users/me/threads/[threadId].delete();
}

@test:Config {
}
function testUrlDecodeFailure() returns error? {
oas:Message receivedMsg = {
threadId: "qweqweqdqd",
id: "saDSASDASDA",
raw: "ASDADSADADADADADAD"
};
Message|error result = convertOASMessageToMessage(receivedMsg);
if result is error {
test:assertEquals(result.message(), "Returned message raw field is not a valid Base64 URL encoded value.",
msg = "Error decoding message");
} else {
test:assertFail("Expected decoded error");
}
}

@test:Config {
}
function testAttachmentSendFailure() returns error? {
MessageRequest sendMsg = {
attachments: [
{
name: "test.txt",
path: "asdadsa",
mimeType: "text/plain"
}
]
};
oas:Message|error result = convertMessageRequestToOASMessage(sendMsg);
if result is error {
test:assertEquals(result.message(),
"Unable to retrieve attachment: asdadsa", msg = "Error decoding message");
} else {
test:assertFail("Expected decoded error");
}
}
8 changes: 6 additions & 2 deletions ballerina/utils.bal
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ isolated function base64UrlEncode(string contentToBeEncoded) returns string {
}

isolated function base64UrlDecode(string contentToBeDecoded) returns string|error {
string base64Encoded = re `_`.replaceAll(re `-`.replaceAll(contentToBeDecoded, PLUS), FORWARD_SLASH);
return check string:fromBytes(check array:fromBase64(base64Encoded));
do {
string base64Encoded = re `_`.replaceAll(re `-`.replaceAll(contentToBeDecoded, PLUS), FORWARD_SLASH);
return check string:fromBytes(check array:fromBase64(base64Encoded));
} on fail error e {
return error InvalidEncodedValue(" is not a valid Base64 URL encoded value.", e);
}
}

0 comments on commit 8941807

Please sign in to comment.