@@ -89,9 +89,11 @@ export class LocalQueryRun {
8989 public constructor (
9090 private readonly outputDir : QueryOutputDir ,
9191 private readonly localQueries : LocalQueries ,
92- public readonly queryInfo : LocalQueryInfo ,
92+ private readonly queryInfo : LocalQueryInfo ,
9393 private readonly dbItem : DatabaseItem ,
94- public readonly logger : Logger ,
94+ public readonly logger : Logger , // Public so that other clients, like the debug adapter, know where to send log output
95+ private readonly queryHistoryManager : QueryHistoryManager ,
96+ private readonly cliServer : CodeQLCliServer ,
9597 ) { }
9698
9799 /**
@@ -112,17 +114,23 @@ export class LocalQueryRun {
112114 this . queryInfo . setEvaluatorLogPaths ( evalLogPaths ) ;
113115 }
114116 const queryWithResults = await this . getCompletedQueryInfo ( results ) ;
115- this . localQueries . queryHistoryManager . completeQuery (
116- this . queryInfo ,
117- queryWithResults ,
118- ) ;
117+ this . queryHistoryManager . completeQuery ( this . queryInfo , queryWithResults ) ;
119118 await this . localQueries . showResultsForCompletedQuery (
120119 this . queryInfo as CompletedLocalQueryInfo ,
121120 WebviewReveal . Forced ,
122121 ) ;
123122 // Note we must update the query history view after showing results as the
124123 // display and sorting might depend on the number of results
125- await this . localQueries . queryHistoryManager . refreshTreeView ( ) ;
124+ await this . queryHistoryManager . refreshTreeView ( ) ;
125+ }
126+
127+ /**
128+ * Updates the UI in the case where query evaluation throws an exception.
129+ */
130+ public async fail ( err : Error ) : Promise < void > {
131+ err . message = `Error running query: ${ err . message } ` ;
132+ this . queryInfo . failureReason = err . message ;
133+ await this . queryHistoryManager . refreshTreeView ( ) ;
126134 }
127135
128136 /**
@@ -134,7 +142,7 @@ export class LocalQueryRun {
134142 logger : BaseLogger ,
135143 ) : Promise < EvaluatorLogPaths | undefined > {
136144 const evalLogPaths = await generateEvalLogSummaries (
137- this . localQueries . cliServer ,
145+ this . cliServer ,
138146 outputDir ,
139147 ) ;
140148 if ( evalLogPaths !== undefined ) {
@@ -166,7 +174,7 @@ export class LocalQueryRun {
166174 ) : Promise < QueryWithResults > {
167175 // Read the query metadata if possible, to use in the UI.
168176 const metadata = await tryGetQueryMetadata (
169- this . localQueries . cliServer ,
177+ this . cliServer ,
170178 this . queryInfo . initialInfo . queryPath ,
171179 ) ;
172180 const query = new QueryEvaluationInfo (
@@ -179,12 +187,9 @@ export class LocalQueryRun {
179187
180188 if ( results . resultType !== QueryResultType . SUCCESS ) {
181189 const message = results . message
182- ? redactableError `${ results . message } `
190+ ? redactableError `Failed to run query: ${ results . message } `
183191 : redactableError `Failed to run query` ;
184- void extLogger . log ( message . fullMessage ) ;
185- void showAndLogExceptionWithTelemetry (
186- redactableError `Failed to run query: ${ message } ` ,
187- ) ;
192+ void showAndLogExceptionWithTelemetry ( message ) ;
188193 }
189194 const message = formatResultMessage ( results ) ;
190195 const successful = results . resultType === QueryResultType . SUCCESS ;
@@ -209,9 +214,9 @@ export class LocalQueries extends DisposableObject {
209214 public constructor (
210215 private readonly app : App ,
211216 private readonly queryRunner : QueryRunner ,
212- public readonly queryHistoryManager : QueryHistoryManager ,
217+ private readonly queryHistoryManager : QueryHistoryManager ,
213218 private readonly databaseManager : DatabaseManager ,
214- public readonly cliServer : CodeQLCliServer ,
219+ private readonly cliServer : CodeQLCliServer ,
215220 private readonly databaseUI : DatabaseUI ,
216221 private readonly localQueryResultsView : ResultsView ,
217222 private readonly queryStorageDir : string ,
@@ -399,7 +404,15 @@ export class LocalQueries extends DisposableObject {
399404 this . queryHistoryManager . addQuery ( queryInfo ) ;
400405
401406 const logger = new TeeLogger ( this . queryRunner . logger , outputDir . logPath ) ;
402- return new LocalQueryRun ( outputDir , this , queryInfo , dbItem , logger ) ;
407+ return new LocalQueryRun (
408+ outputDir ,
409+ this ,
410+ queryInfo ,
411+ dbItem ,
412+ logger ,
413+ this . queryHistoryManager ,
414+ this . cliServer ,
415+ ) ;
403416 }
404417
405418 public async compileAndRunQuery (
@@ -478,13 +491,15 @@ export class LocalQueries extends DisposableObject {
478491
479492 return results ;
480493 } catch ( e ) {
494+ // It's odd that we have two different ways for a query evaluation to fail: by throwing an
495+ // exception, and by returning a result with a failure code. This is how the code worked
496+ // before the refactoring, so it's been preserved, but we should probably figure out how
497+ // to unify both error handling paths.
481498 const err = asError ( e ) ;
482- err . message = `Error running query: ${ err . message } ` ;
483- localQueryRun . queryInfo . failureReason = err . message ;
499+ await localQueryRun . fail ( err ) ;
484500 throw e ;
485501 }
486502 } finally {
487- await this . queryHistoryManager . refreshTreeView ( ) ;
488503 source . dispose ( ) ;
489504 }
490505 }
0 commit comments