66using System . Collections . Frozen ;
77using System . Text . Json ;
88using Elastic . Documentation . Configuration ;
9+ using Elastic . Documentation . Diagnostics ;
910using Elastic . Documentation . LinkIndex ;
1011using Elastic . Documentation . Links ;
1112using Elastic . Documentation . Serialization ;
@@ -32,7 +33,7 @@ public record FetchedCrossLinks
3233 } ;
3334}
3435
35- public abstract class CrossLinkFetcher ( ILoggerFactory logFactory , ILinkIndexReader linkIndexProvider ) : IDisposable
36+ public abstract class CrossLinkFetcher ( ILoggerFactory logFactory , ILinkIndexReader linkIndexProvider , IDiagnosticsCollector collector ) : IDisposable
3637{
3738 private readonly ILogger _logger = logFactory . CreateLogger ( nameof ( CrossLinkFetcher ) ) ;
3839 private readonly HttpClient _client = new ( ) ;
@@ -60,32 +61,40 @@ protected async Task<LinkRegistryEntry> GetLinkIndexEntry(string repository, Can
6061 {
6162 var linkIndex = await FetchLinkIndex ( ctx ) ;
6263 if ( ! linkIndex . Repositories . TryGetValue ( repository , out var repositoryLinks ) )
64+ {
65+ collector . EmitError ( repository , $ "Repository { repository } not found in link index") ;
6366 throw new Exception ( $ "Repository { repository } not found in link index") ;
67+ }
68+
6469 return GetNextContentSourceLinkIndexEntry ( repositoryLinks , repository ) ;
6570 }
6671
67- protected static LinkRegistryEntry GetNextContentSourceLinkIndexEntry ( IDictionary < string , LinkRegistryEntry > repositoryLinks , string repository )
72+ protected LinkRegistryEntry GetNextContentSourceLinkIndexEntry ( IDictionary < string , LinkRegistryEntry > repositoryLinks , string repository )
6873 {
69- var linkIndexEntry =
70- ( repositoryLinks . TryGetValue ( "main" , out var link )
71- ? link
72- : repositoryLinks . TryGetValue ( "master" , out link ) ? link : null )
73- ?? throw new Exception ( $ "Repository { repository } found in link index, but no main or master branch found") ;
74- return linkIndexEntry ;
74+ var linkIndexEntry = repositoryLinks . TryGetValue ( "main" , out var link ) ? link : repositoryLinks . TryGetValue ( "master" , out link ) ? link : null ;
75+ if ( linkIndexEntry is not null )
76+ return linkIndexEntry ;
77+
78+ collector . EmitError ( repository , $ "Repository found in link index however neither ' main' nor ' master' branches found") ;
79+ throw new Exception ( $ "Repository { repository } found in link index, but no main or master branch found" ) ;
7580 }
7681
7782 protected async Task < RepositoryLinks > Fetch ( string repository , string [ ] keys , Cancel ctx )
7883 {
7984 var linkIndex = await FetchLinkIndex ( ctx ) ;
8085 if ( ! linkIndex . Repositories . TryGetValue ( repository , out var repositoryLinks ) )
86+ {
87+ collector . EmitError ( repository , $ "Repository { repository } not found in link index") ;
8188 throw new Exception ( $ "Repository { repository } not found in link index") ;
89+ }
8290
8391 foreach ( var key in keys )
8492 {
8593 if ( repositoryLinks . TryGetValue ( key , out var linkIndexEntry ) )
8694 return await FetchLinkIndexEntry ( repository , linkIndexEntry , ctx ) ;
8795 }
8896
97+ collector . EmitError ( repository , $ "Repository found in link index however none of: '{ string . Join ( ", " , keys ) } ' branches found") ;
8998 throw new Exception ( $ "Repository found in link index however none of: '{ string . Join ( ", " , keys ) } ' branches found") ;
9099 }
91100
@@ -97,7 +106,17 @@ protected async Task<RepositoryLinks> FetchLinkIndexEntry(string repository, Lin
97106
98107 var url = $ "https://elastic-docs-link-index.s3.us-east-2.amazonaws.com/{ linkRegistryEntry . Path } ";
99108 _logger . LogInformation ( "Fetching links.json for '{Repository}': {Url}" , repository , url ) ;
100- var json = await _client . GetStringAsync ( url , ctx ) ;
109+ string json ;
110+ try
111+ {
112+ json = await _client . GetStringAsync ( url , ctx ) ;
113+ }
114+ catch ( Exception e )
115+ {
116+ collector . EmitError ( repository , $ "An error occurred fetching links.json for '{ repository } ' from '{ url } ': { e . Message } ") ;
117+ throw new Exception ( $ "An error occurred fetching links.json for '{ repository } ' from '{ url } ': { e . Message } ", e ) ;
118+ }
119+
101120 linkReference = Deserialize ( json ) ;
102121 WriteLinksJsonCachedFile ( repository , linkRegistryEntry , json ) ;
103122 return linkReference ;
0 commit comments