-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathdiagnosis-formatting.ts
More file actions
173 lines (145 loc) · 5.98 KB
/
Copy pathdiagnosis-formatting.ts
File metadata and controls
173 lines (145 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { sideBySide, TreeBuilder, wrapText } from './tree-builder';
import type { DiagnosedStack, StackDiagnosis, StackProblemSource, TracedResourceError } from '../../actions/diagnose';
import { DeploymentError, ToolkitError } from '../../toolkit/toolkit-error';
import { sortByKey } from '../../util';
import type { ActionLessMessage } from '../io/private';
import { IO } from '../io/private';
/**
* Turn the given stack diagnosis result into an IO message, with default message formatting
*
* The default message formatting is what the CLI will print.
*/
export function hostMessageFromDiagnosis(stack: DiagnosedStack): ActionLessMessage<any> {
const diagnosis = stack.result;
switch (diagnosis.type) {
case 'no-problem':
return IO.CDK_TOOLKIT_I9500.msg(formatNoProblemStack(stack, diagnosis), stack);
case 'problem':
return IO.CDK_TOOLKIT_E9500.msg(formatProblemStack(stack, diagnosis), stack);
case 'error-diagnosing':
return IO.CDK_TOOLKIT_W9501.msg(formatDiagnosisErrorStack(stack, diagnosis), stack);
}
}
/**
* Turn the given diagnosis into a DeploymentError
*/
export function throwDeploymentErrorFromDiagnosis(diag: StackDiagnosis): never {
switch (diag.type) {
case 'no-problem':
throw new ToolkitError('DeploymentErrorNotError', 'Diagnosis should represent an error, but does not');
case 'error-diagnosing':
throw new DeploymentError(diag.message, 'ErrorDiagnosisFailed');
}
// Guaranteed 'type=problem' here
const errorCode = diag.problems[0]?.errorCode;
let defaultErrorCode;
switch (diag.detectedBy.type) {
case 'change-set':
defaultErrorCode = 'ChangeSetCreationFailed';
break;
case 'early-validation':
defaultErrorCode = 'EarlyValidationFailure';
break;
case 'deployment':
defaultErrorCode = 'StackDeployFailed';
break;
}
throw new DeploymentError(formatProblemDiagnosis(diag), errorCode ?? defaultErrorCode);
}
function formatProblemDiagnosis(diag: Extract<StackDiagnosis, { type: 'problem' }>): string {
switch (diag.detectedBy.type) {
case 'change-set':
return formatChangeSetProblems(diag.problems, diag.detectedBy);
case 'early-validation':
return formatEarlyValidationProblems(diag.problems, diag.detectedBy);
case 'deployment':
return formatDeploymentProblems(diag.problems, diag.detectedBy);
}
}
function formatChangeSetProblems(problems: TracedResourceError[], detectedBy: Extract<StackProblemSource, { type: 'change-set' }>): string {
const caption = `Failed to create change set ${detectedBy.changeSetName}`;
if (problems.length > 0) {
return `${caption}:\n${formatResourceErrors(problems)}`;
} else {
return `${caption}: ${detectedBy.statusReason}`;
}
}
function formatEarlyValidationProblems(problems: TracedResourceError[], detectedBy: Extract<StackProblemSource, { type: 'early-validation' }>): string {
const caption = `Early validation failed for change set ${detectedBy.changeSetName}`;
if (problems.length > 0) {
return `${caption}:\n${formatResourceErrors(problems)}`;
} else {
return caption;
}
}
function formatDeploymentProblems(problems: TracedResourceError[], detectedBy: Extract<StackProblemSource, { type: 'deployment' }>): string {
const caption = 'Resource updates failed';
if (problems.length > 0) {
return `${caption}:\n${formatResourceErrors(problems)}`;
} else {
return `${caption} (${detectedBy.stackStatus}): ${detectedBy.statusReason}`;
}
}
function formatNoProblemStack(stack: DiagnosedStack, _m: Extract<StackDiagnosis, { type: 'no-problem' }>) {
// TODO: print stack by construct path, not name
return `✅ Stack ${stack.stackName}: no issues found.`;
}
function formatProblemStack(stack: DiagnosedStack, m: Extract<StackDiagnosis, { type: 'problem' }>) {
// TODO: print stack by construct path, not name
return `❌ Stack ${stack.stackName}:\n${formatProblemDiagnosis(m)}`;
}
function formatDiagnosisErrorStack(stack: DiagnosedStack, m: Extract<StackDiagnosis, { type: 'error-diagnosing' }>) {
// TODO: print stack by construct path, not name
return `⚠️ Could not diagnose stack ${stack.stackName}: ${m.message}`;
}
function formatResourceErrors(es: TracedResourceError[]) {
sortByKey(es, (e) => [locateResourceError(e)]);
const b = new TreeBuilder('');
for (const e of es) {
const p = locateResourceError(e);
const lastPart = p.split('/').slice(-1)[0];
const nodeText = b.nodeText(p);
nodeText.header = [`${lastPart} ${addendum(' ', e.resourceType, e.logicalId)}`.trim()];
nodeText.body.push(...sideBySide(['🛑'], ' ', wrapText(120, e.message)));
nodeText.footer = e.sourceTrace?.creationStackTrace
? sideBySide(['Source Location:'], ' ', e.sourceTrace?.creationStackTrace)
: [];
if (e.additionalContext) {
for (const ctx of e.additionalContext) {
const ctxNode = b.nodeText(`${p}/${ctx.source.replace(/\//g, '|')}`);
ctxNode.header = [`📋 ${ctx.source}:`];
for (const msg of ctx.messages) {
ctxNode.body.push(msg);
}
if (ctx.link) {
ctxNode.body.push(`🔗 ${ctx.link}`);
}
}
}
}
return b.render();
}
/**
* Return a /-separated construct path for the given error, or try to build as close a represention as possible if we don't have a construct path
*
* The root will be the "app"
*/
function locateResourceError(e: TracedResourceError): string {
if (e.sourceTrace?.constructPath) {
return e.sourceTrace?.constructPath;
}
const nestedStackParts = e.parentStackLogicalIds.map((l) => `Nested stack ${l}`);
if (e.logicalId) {
return [e.topLevelStackHierarchicalId, ...nestedStackParts, e.logicalId].join('/');
}
// No logical ID means we are targeting the stack itself
return [e.topLevelStackHierarchicalId, nestedStackParts].join('/');
}
function addendum(sep: string, ...xs: Array<string | undefined>): string {
xs = xs.filter(x => x && typeof x === 'string');
if (xs.length > 0) {
return `(${xs.join(sep)})`;
} else {
return '';
}
}