Skip to content

Commit

Permalink
Update error handling in function
Browse files Browse the repository at this point in the history
  • Loading branch information
suny-am committed May 1, 2024
1 parent 40bf02d commit b3b2221
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions GithubDataProcessor/WriteGithubData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public async Task Run([TimerTrigger("0 0 20 * * 0,1,2,3,4")] TimerInfo myTimer)
$"Write operations from" +
$"Github to CosmosDB started at: {DateTime.Now}");

bool operationCompleted = false;
try
{
string ghApiToken = await _tokenClient.GetStringAsync(Environment
.GetEnvironmentVariable("GITHUB_TOKEN_ENDPOINT"));
.GetEnvironmentVariable("GITHUB_TOKEN_ENDPOINT"))
?? throw new HttpRequestException("Could not get Github Access Token");

_graphQLHandler = new(ghApiToken);

Expand All @@ -37,8 +37,7 @@ public async Task Run([TimerTrigger("0 0 20 * * 0,1,2,3,4")] TimerInfo myTimer)
AuthenticatedRequest request = new(query, variables);

ResponseType? response = await _graphQLHandler
.PerformQuery(request)
?? throw new Exception("Query failed");
.PerformQuery(request);

IEnumerable<RepositoryType?>? repositories = response?
.Search?
Expand All @@ -61,22 +60,18 @@ public async Task Run([TimerTrigger("0 0 20 * * 0,1,2,3,4")] TimerInfo myTimer)
));

await _cosmosDBhandler.UnitOfWork(repositoryItems, "CREATE");
operationCompleted = true;
}
catch (Exception ex)
{
_logger.LogError("{Message}", $"Could not complete write operations: {ex.Message}");
_logger.LogError("{Message}", $"Could not complete write operations: {ex}");
}
finally
{
string operationStatus = operationCompleted ? "completed" : "failed";

if (myTimer.ScheduleStatus is not null)
{
_logger.LogInformation("{Message}", $" Operation {operationStatus}. " +
$"Next timer scheduled to: {myTimer.ScheduleStatus.Next}");
}
if (myTimer.ScheduleStatus is not null)
{
_logger.LogInformation("{Message}", $" Operation completed. " +
$"Next timer scheduled to: {myTimer.ScheduleStatus.Next}");
}

}
}
}

0 comments on commit b3b2221

Please sign in to comment.