I have the following method:
private async Task<(bool, string)> Relay(
WorkflowTask workflowTask,
MontageUploadConfig montageData,
File sourceFile,
CancellationToken cancellationToken
)
{
try
{
byte[] fileContent = await _httpClient.GetByteArrayAsync(sourceFile.Url, cancellationToken);
await _attachmentController.TryUploadAttachment(montageData.EventId, fileContent, sourceFile.Name);
return (true, null);
}
catch (Exception exception)
{
_logger.LogError(exception, $"File cannot be uploaded: {sourceFile.Name}", workflowTask);
return (false, exception.ToString());
}
}
I'd like to refactor it to use TryAsync
from LanguageExt.Core
(or some other functional Try
type).
I've managed to refactor the above method to:
private TryAsync<bool> Relay(
MontageUploadConfig montageData,
File sourceFile,
CancellationToken cancellationToken
) => new(async () =>
{
byte[] fileContent = await _httpClient.GetByteArrayAsync(sourceFile.Url, cancellationToken);
return await _attachmentController.TryUploadAttachment(montageData.EventId, fileContent, sourceFile.Name);
});
This compiles, but I haven't been able how to consume the result, either doing whatever comes next or logging the exception.
How can I check the result and either do something with either the returned value or any exceptions?