@@ -7,20 +7,11 @@ import { ProjectGraph } from '../config/project-graph';
7
7
import { CreateNodesFunctionV2 } from './plugins/public-api' ;
8
8
9
9
export class ProjectGraphError extends Error {
10
- readonly #errors: Array <
11
- | AggregateCreateNodesError
12
- | MergeNodesError
13
- | CreateMetadataError
14
- | ProjectsWithNoNameError
15
- | MultipleProjectsWithSameNameError
16
- | ProcessDependenciesError
17
- | WorkspaceValidityError
18
- > ;
19
10
readonly #partialProjectGraph: ProjectGraph ;
20
11
readonly #partialSourceMaps: ConfigurationSourceMaps ;
21
12
22
13
constructor (
23
- errors : Array <
14
+ private readonly errors : Array <
24
15
| AggregateCreateNodesError
25
16
| MergeNodesError
26
17
| ProjectsWithNoNameError
@@ -32,16 +23,46 @@ export class ProjectGraphError extends Error {
32
23
partialProjectGraph : ProjectGraph ,
33
24
partialSourceMaps : ConfigurationSourceMaps
34
25
) {
35
- super (
36
- `Failed to process project graph. Run "nx reset" to fix this. Please report the issue if you keep seeing it.`
37
- ) ;
26
+ const messageFragments = [ 'Failed to process project graph.' ] ;
27
+ const mergeNodesErrors = [ ] ;
28
+ const unknownErrors = [ ] ;
29
+ for ( const e of errors ) {
30
+ if (
31
+ // Known errors that are self-explanatory
32
+ isAggregateCreateNodesError ( e ) ||
33
+ isCreateMetadataError ( e ) ||
34
+ isProcessDependenciesError ( e ) ||
35
+ isProjectsWithNoNameError ( e ) ||
36
+ isMultipleProjectsWithSameNameError ( e ) ||
37
+ isWorkspaceValidityError ( e )
38
+ ) {
39
+ } else if (
40
+ // Known error type, but unlikely to be caused by the user
41
+ isMergeNodesError ( e )
42
+ ) {
43
+ mergeNodesErrors . push ( e ) ;
44
+ } else {
45
+ unknownErrors . push ( e ) ;
46
+ }
47
+ }
48
+ if ( mergeNodesErrors . length > 0 ) {
49
+ messageFragments . push (
50
+ `This type of error most likely points to an issue within Nx. Please report it.`
51
+ ) ;
52
+ }
53
+ if ( unknownErrors . length > 0 ) {
54
+ messageFragments . push (
55
+ `If the error cause is not obvious from the below error messages, running "nx reset" may fix it. Please report the issue if you keep seeing it.`
56
+ ) ;
57
+ }
58
+ super ( messageFragments . join ( ' ' ) ) ;
38
59
this . name = this . constructor . name ;
39
- this . # errors = errors ;
60
+ this . errors = errors ;
40
61
this . #partialProjectGraph = partialProjectGraph ;
41
62
this . #partialSourceMaps = partialSourceMaps ;
42
- this . stack = ` ${ this . message } \n ${ errors
63
+ this . stack = errors
43
64
. map ( ( error ) => indentString ( formatErrorStackAndCause ( error ) , 2 ) )
44
- . join ( '\n' ) } ` ;
65
+ . join ( '\n' ) ;
45
66
}
46
67
47
68
/**
@@ -67,7 +88,7 @@ export class ProjectGraphError extends Error {
67
88
}
68
89
69
90
getErrors ( ) {
70
- return this . # errors;
91
+ return this . errors ;
71
92
}
72
93
}
73
94
@@ -242,6 +263,36 @@ export class AggregateCreateNodesError extends Error {
242
263
}
243
264
}
244
265
266
+ export function formatAggregateCreateNodesError (
267
+ error : AggregateCreateNodesError ,
268
+ pluginName : string
269
+ ) {
270
+ const errorBodyLines = [
271
+ `${
272
+ error . errors . length > 1 ? `${ error . errors . length } errors` : 'An error'
273
+ } occurred while processing files for the ${ pluginName } plugin.`,
274
+ ] ;
275
+ const errorStackLines = [ ] ;
276
+
277
+ const innerErrors = error . errors ;
278
+ for ( const [ file , e ] of innerErrors ) {
279
+ if ( file ) {
280
+ errorBodyLines . push ( ` - ${ file } : ${ e . message } ` ) ;
281
+ errorStackLines . push ( ` - ${ file } : ${ e . stack } ` ) ;
282
+ } else {
283
+ errorBodyLines . push ( ` - ${ e . message } ` ) ;
284
+ errorStackLines . push ( ` - ${ e . stack } ` ) ;
285
+ }
286
+ if ( e . stack && process . env . NX_VERBOSE_LOGGING === 'true' ) {
287
+ const innerStackTrace = ' ' + e . stack . split ( '\n' ) ?. join ( '\n ' ) ;
288
+ errorStackLines . push ( innerStackTrace ) ;
289
+ }
290
+ }
291
+
292
+ error . stack = errorStackLines . join ( '\n' ) ;
293
+ error . message = errorBodyLines . join ( '\n' ) ;
294
+ }
295
+
245
296
export class MergeNodesError extends Error {
246
297
file : string ;
247
298
pluginName : string ;
@@ -292,6 +343,16 @@ export class ProcessDependenciesError extends Error {
292
343
this . stack = `${ this . message } \n ${ cause . stack . split ( '\n' ) . join ( '\n ' ) } ` ;
293
344
}
294
345
}
346
+
347
+ function isProcessDependenciesError ( e : unknown ) : e is ProcessDependenciesError {
348
+ return (
349
+ e instanceof ProcessDependenciesError ||
350
+ ( typeof e === 'object' &&
351
+ 'name' in e &&
352
+ e ?. name === ProcessDependenciesError . name )
353
+ ) ;
354
+ }
355
+
295
356
export class WorkspaceValidityError extends Error {
296
357
constructor ( public message : string ) {
297
358
message = `Configuration Error\n${ message } ` ;
0 commit comments