@@ -54,7 +54,8 @@ public async Task<HealthCheckResult> CheckHealthAsync(
5454 _logger . LogDebug ( "Checking GOV.UK Notify connectivity by fetching templates" ) ;
5555
5656 var client = new NotificationClient ( _config . ApiKey ) ;
57- var templates = await client . GetAllTemplatesAsync ( ) ;
57+ var templates = await client . GetAllTemplatesAsync ( )
58+ . WaitAsync ( TimeSpan . FromSeconds ( 30 ) , cancellationToken ) ;
5859
5960 var templateCount = templates . templates ? . Count ?? 0 ;
6061 data [ "TemplateCount" ] = templateCount ;
@@ -85,15 +86,28 @@ public async Task<HealthCheckResult> CheckHealthAsync(
8586 }
8687 catch ( NotifyClientException ex )
8788 {
88- _logger . LogError ( ex , "GOV.UK Notify client error during health check" ) ;
89- data [ "Error" ] = ex . Message ;
90- return HealthCheckResult . Unhealthy ( $ "Client error: { ex . Message } ", ex , data ) ;
89+ var fullMessage = GetFullExceptionMessage ( ex ) ;
90+ _logger . LogError ( ex , "GOV.UK Notify client error during health check: {ErrorMessage}" , fullMessage ) ;
91+ data [ "Error" ] = fullMessage ;
92+ return HealthCheckResult . Unhealthy ( $ "Client error: { fullMessage } ", ex , data ) ;
93+ }
94+ catch ( TimeoutException ex )
95+ {
96+ _logger . LogWarning ( ex , "GOV.UK Notify health check timed out" ) ;
97+ data [ "Error" ] = "Request timed out" ;
98+ return HealthCheckResult . Degraded ( "Request timed out - service may be slow or unreachable" , data : data ) ;
99+ }
100+ catch ( OperationCanceledException ) when ( cancellationToken . IsCancellationRequested )
101+ {
102+ _logger . LogInformation ( "GOV.UK Notify health check was cancelled" ) ;
103+ throw ; // Let the health check system handle cancellation
91104 }
92105 catch ( Exception ex )
93106 {
94- _logger . LogError ( ex , "Unexpected error during GOV.UK Notify health check" ) ;
95- data [ "Error" ] = ex . Message ;
96- return HealthCheckResult . Unhealthy ( $ "Unexpected error: { ex . Message } ", ex , data ) ;
107+ var fullMessage = GetFullExceptionMessage ( ex ) ;
108+ _logger . LogError ( ex , "Unexpected error during GOV.UK Notify health check: {ErrorMessage}" , fullMessage ) ;
109+ data [ "Error" ] = fullMessage ;
110+ return HealthCheckResult . Unhealthy ( $ "Unexpected error: { fullMessage } ", ex , data ) ;
97111 }
98112 }
99113
@@ -105,4 +119,18 @@ private static string HashApiKey(string apiKey)
105119 var bytes = SHA256 . HashData ( Encoding . UTF8 . GetBytes ( apiKey ) ) ;
106120 return Convert . ToHexString ( bytes ) [ ..16 ] . ToLowerInvariant ( ) ;
107121 }
122+
123+ private static string GetFullExceptionMessage ( Exception ex )
124+ {
125+ var messages = new List < string > ( ) ;
126+ var current = ex ;
127+
128+ while ( current != null )
129+ {
130+ messages . Add ( current . Message ) ;
131+ current = current . InnerException ;
132+ }
133+
134+ return string . Join ( " --> " , messages ) ;
135+ }
108136}
0 commit comments