44using LineCount . Errors ;
55using LineCount . Logging ;
66using LineCount . Result ;
7+ using System . Threading . Tasks ;
78
89namespace LineCount ;
910
@@ -129,6 +130,11 @@ static async Task<ReportResult> CountInDirectories(string path, LineCountData da
129130
130131 await foreach ( var result in Task . WhenEach ( directorytasks ) )
131132 {
133+ if ( ! result . IsCompletedSuccessfully )
134+ {
135+ return HandleTaskFailure ( result ) ;
136+ }
137+
132138 if ( ! result . Result . TryGetValue ( out LineCountReport ? report ) )
133139 {
134140 return ReportResult . Failure ( result . Result . Error ) ;
@@ -144,11 +150,26 @@ static async Task<ReportResult> CountInDirectories(string path, LineCountData da
144150 return new LineCountReport ( lineCount , fileCount ) ;
145151 }
146152
153+ static ReportResult HandleTaskFailure < T > ( Task < T > result )
154+ {
155+ if ( result . IsCanceled )
156+ {
157+ throw new OperationCanceledException ( ) ;
158+ }
159+
160+ if ( result . IsFaulted )
161+ {
162+ return new UndiagnosedError ( result . Exception ) ;
163+ }
164+
165+ return new InternalError ( "Task has not been cancelled or faulted nor completed successfully" ) ;
166+ }
167+
147168 static async Task < ReportResult > CountInFiles ( string path , LineCountData data , PathPatterns excludeFilePatterns , CancellationToken cancellationToken = default )
148169 {
149170 List < Task < Result < FileStats , IError > > > filetasks = [ ] ;
150-
151171 try
172+
152173 {
153174 IEnumerable < string > files = GetFilterFilePaths ( path , data ) ;
154175
@@ -160,7 +181,7 @@ static async Task<ReportResult> CountInFiles(string path, LineCountData data, Pa
160181 }
161182
162183 Task < Result < FileStats , IError > > task = GetSingleFileLineCount ( file , data , cancellationToken )
163- . ContinueWith ( task => task . Result . Map ( report => new FileStats ( file , report . Lines ) ) ) ;
184+ . ContinueWith ( task => task . Result . Map ( report => new FileStats ( file , report . Lines ) ) , cancellationToken , TaskContinuationOptions . NotOnCanceled , TaskScheduler . Current ) ;
164185 filetasks . Add ( task ) ;
165186 }
166187 }
@@ -194,7 +215,12 @@ static async Task<ReportResult> CountInFiles(string path, LineCountData data, Pa
194215
195216 await foreach ( var result in Task . WhenEach ( filetasks ) )
196217 {
197- if ( ! result . Result . TryGetValue ( out FileStats ? fileStats ) )
218+ if ( ! result . IsCompletedSuccessfully )
219+ {
220+ return HandleTaskFailure ( result ) ;
221+ }
222+
223+ if ( ! result . Result . TryGetValue ( out FileStats ? fileStats ) )
198224 {
199225 return ReportResult . Failure ( result . Result . Error ) ;
200226 }
@@ -233,7 +259,7 @@ static Task<LineCountReport> GetSingleFileLineCountReport(string path, LineCount
233259 FilterType . FilteredExcept => GetFilteredFileLineCount ( path , line => ! data . ExcludeLineFilter ! . IsMatch ( line ) , cancellationToken ) ,
234260 FilterType . FilteredBoth => GetFilteredFileLineCount ( path , line => data . LineFilter ! . IsMatch ( line ) && ! data . ExcludeLineFilter ! . IsMatch ( line ) , cancellationToken ) ,
235261 _ => throw new InvalidOperationException ( $ "CountType.{ data . FilterType } not recognized") ,
236- } ) . ContinueWith ( task => new LineCountReport ( task . Result ) ) ;
262+ } ) . ContinueWith ( task => new LineCountReport ( task . Result ) , cancellationToken , TaskContinuationOptions . NotOnCanceled , TaskScheduler . Current ) ;
237263 }
238264
239265 static async Task < ReportResult > GetSingleFileLineCount ( string path , LineCountData data , CancellationToken cancellationToken = default )
0 commit comments