Skip to content

Commit 1590fe9

Browse files
calin-lupas_dsamcapscalin-lupas_dsamcaps
authored andcommitted
#25 Improve logging and error handling in services
Updated logging in DevExPullRequestEventProcessorService to use consistent variable names and added detailed logs for pull request state and merge commit SHA. Enhanced error handling in DevExWebhookEventProcessorService by introducing a try-catch block, moving null checks inside it, and improving logging for unhandled exceptions.
1 parent da21908 commit 1590fe9

2 files changed

Lines changed: 21 additions & 11 deletions

File tree

src/DevExcelerateApi/Services/DevExPullRequestEventProcessorService.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public async Task SaveAndProcessPullRequestEventAsync(WebhookHeaders headers, Pu
3535

3636
default:
3737
_logger.LogInformation("Not supported action for processing the pull request.");
38+
break;
3839
}
3940

4041
_logger.LogInformation("GitHub Webhook event saved to database and processed for PR# {number} with action - {action}...", (pullRequestEvent?.PullRequest.Number), pullRequestEvent?.Action);
@@ -44,18 +45,19 @@ private async Task ValidatePullRequestApproval(PullRequestEvent pullRequestEvent
4445
{
4546
var prNumber = (pullRequestEvent?.PullRequest.Number).GetValueOrDefault();
4647

48+
_logger.LogInformation("Pull request #{prNumber} is {State} with merge commit SHA {MergeCommitSha}.", prNumber, pullRequestEvent?.PullRequest.State, pullRequestEvent?.PullRequest.MergeCommitSha);
49+
4750
// Closed & Merged
4851
if (pullRequestEvent?.PullRequest.State == PullRequestState.Closed
49-
&& pullRequestEvent?.PullRequest.MergedAt != null
5052
&& pullRequestEvent?.PullRequest.MergeCommitSha != null)
5153
{
52-
_logger.LogInformation("Pull request #{number} is closed and merged.", prNumber);
54+
_logger.LogInformation("Pull request #{prNumber} is closed and merged.", prNumber);
5355

5456
await _repositoryService.SaveRepository(pullRequestEvent!);
5557
}
5658
else
5759
{
58-
_logger.LogInformation("Pull request #{number} is still in review.", prNumber);
60+
_logger.LogInformation("Pull request #{prNumber} is still in review.", prNumber);
5961
}
6062
}
6163
}

src/DevExcelerateApi/Services/DevExWebhookEventProcessorService.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,27 @@ public class DevExWebhookEventProcessorService(
5454

5555
public override Task ProcessWebhookAsync(IDictionary<string, StringValues> headers, string body)
5656
{
57-
ArgumentNullException.ThrowIfNull(headers);
58-
ArgumentNullException.ThrowIfNull(body);
57+
try
58+
{
59+
ArgumentNullException.ThrowIfNull(headers);
60+
ArgumentNullException.ThrowIfNull(body);
61+
62+
var webhookHeaders = WebhookHeaders.Parse(headers);
5963

60-
var webhookHeaders = WebhookHeaders.Parse(headers);
64+
// Bypass if not an implemented event
65+
if (!_allowedWebhookEvents.Contains(webhookHeaders?.Event!))
66+
{
67+
_logger.LogInformation("--> Bypassing GitHub Webhook event {Event}", webhookHeaders?.Event);
68+
return Task.CompletedTask;
69+
}
6170

62-
// Bypass if not an implemented event
63-
if (!_allowedWebhookEvents.Contains(webhookHeaders?.Event!))
71+
return base.ProcessWebhookAsync(headers, body);
72+
}
73+
catch (Exception ex)
6474
{
65-
_logger.LogInformation("--> Bypassing GitHub Webhook event {Event}", webhookHeaders?.Event);
75+
_logger.LogError(ex, "Error processing GitHub webhook event.");
6676
return Task.CompletedTask;
6777
}
68-
69-
return base.ProcessWebhookAsync(headers, body);
7078
}
7179

7280
protected override Task ProcessIssuesWebhookAsync(WebhookHeaders headers, IssuesEvent issuesEvent, IssuesAction action)

0 commit comments

Comments
 (0)