-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
handle-error.js
234 lines (208 loc) · 8.84 KB
/
handle-error.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
'use strict';
const path = require('path');
const isObject = require('type/object/is');
const chalk = require('chalk');
const stripAnsi = require('strip-ansi');
const sfeVersion = require('@serverless/dashboard-plugin/package.json').version;
const { platformClientVersion } = require('@serverless/dashboard-plugin');
const { style, writeText, legacy, log } = require('@serverless/utils/log');
const slsVersion = require('./../../package').version;
const { logWarning } = require('../classes/Error');
const isStandaloneExecutable = require('../utils/isStandaloneExecutable');
const tokenizeException = require('../utils/tokenize-exception');
const logDeprecation = require('../utils/logDeprecation');
const resolveIsLocallyInstalled = require('../utils/is-locally-installed');
const isTelemetryDisabled = require('../utils/telemetry/areDisabled');
const { storeLocally: storeTelemetryLocally, send: sendTelemetry } = require('../utils/telemetry');
const generateTelemetryPayload = require('../utils/telemetry/generatePayload');
const resolveErrorLocation = require('../utils/telemetry/resolve-error-location');
const resolveInput = require('./resolve-input');
const resolveLocalServerlessPath = require('./resolve-local-serverless-path');
const writeMessage = (title, message) => {
let line = '';
while (line.length < 56 - title.length) {
line = `${line}-`;
}
legacy.consoleLog(' ');
legacy.consoleLog(chalk.yellow(` ${title} ${line}`));
legacy.consoleLog(' ');
if (message) {
legacy.consoleLog(` ${message.split('\n').join('\n ')}`);
}
legacy.consoleLog(' ');
};
const isErrorCodeNormative = RegExp.prototype.test.bind(/^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)*$/);
module.exports = async (exception, options = {}) => {
if (!isObject(options)) options = {};
// Due to the fact that the handler can be invoked via fallback, we need to support both `serverless`
// and `isLocallyInstalled` + `isInvokedByGlobalInstallation` properties
// TODO: Support for these properties should be removed with next major
const {
isUncaughtException,
isLocallyInstalled: passedIsLocallyInstalled,
isInvokedByGlobalInstallation: passedIsInvokedByGlobalInstallation,
serverless,
hasTelemetryBeenReported,
commandUsage,
variableSourcesInConfig,
} = options;
let { command, options: cliOptions, commandSchema, serviceDir, configuration } = options;
const isLocallyInstalled = serverless ? serverless.isLocallyInstalled : passedIsLocallyInstalled;
const isInvokedByGlobalInstallation = serverless
? serverless.isInvokedByGlobalInstallation
: passedIsInvokedByGlobalInstallation;
// If provided serverless instance is a local fallback, and we're not in context of it
// Pass error handling to this local fallback implementation
if (isInvokedByGlobalInstallation && !resolveIsLocallyInstalled()) {
const localServerlessPath = resolveLocalServerlessPath();
const localErrorHandlerData = (() => {
try {
return {
handle: require(path.resolve(localServerlessPath, 'lib/cli/handle-error')),
options: {
serverless,
isLocallyInstalled,
isUncaughtException,
command,
options: cliOptions,
commandSchema,
serviceDir,
configuration,
hasTelemetryBeenReported,
commandUsage,
variableSourcesInConfig,
},
};
} catch {
try {
// Ugly mock of serverless below is used to ensure that Framework version will be logged with `(local)` suffix
return {
handle: require(path.resolve(localServerlessPath, 'lib/classes/Error')).logError,
options: {
serverless: serverless || { isLocallyInstalled },
forceExit: isUncaughtException,
},
};
} catch {
// Corner case of local installation being removed during command processing.
// It can happen e.g. during "plugin uninstall" command, where "serverless" originally
// installed a as peer dependency was removed
logWarning('Could not resolve path to locally installed error handler');
log.debug('Could not resolve path to locally installed error handler');
return null;
}
}
})();
if (localErrorHandlerData) {
localErrorHandlerData.handle(exception, localErrorHandlerData.options);
return;
}
}
const exceptionTokens = tokenizeException(exception);
const isUserError = !isUncaughtException && exceptionTokens.isUserError;
writeMessage(
exceptionTokens.title,
exceptionTokens.stack && (!isUserError || process.env.SLS_DEBUG)
? exceptionTokens.stack
: exceptionTokens.message
);
if (!isUserError && !process.env.SLS_DEBUG) {
const debugInfo = [
' ',
' For debugging logs, run again after setting the',
' "SLS_DEBUG=*" environment variable.',
].join('');
legacy.consoleLog(chalk.red(debugInfo));
legacy.consoleLog(' ');
}
const platform = process.platform;
const nodeVersion = process.version.replace(/^[v|V]/, '');
const installationModePostfix = (() => {
if (isStandaloneExecutable) return ' (standalone)';
if (isLocallyInstalled != null) return isLocallyInstalled ? ' (local)' : '';
return resolveIsLocallyInstalled() ? ' (local)' : '';
})();
const componentsVersion = (() => {
try {
return require('@serverless/components/package').version;
} catch (error) {
return 'Unavailable';
}
})();
writeText(
style.aside(
`Environment: ${platform}, node ${nodeVersion}, framework ${slsVersion}${installationModePostfix}, plugin ${sfeVersion}, SDK ${platformClientVersion}`,
'Docs: docs.serverless.com',
'Support: forum.serverless.com',
'Bugs: github.com/serverless/serverless/issues'
)
);
// TODO: Ideally after migrating to new logger complete this strip should not be needed
// (it can be removed after we clear all chalk error message decorations from internals)
const errorMsg = stripAnsi(
exceptionTokens.stack && !isUserError ? exceptionTokens.stack : exceptionTokens.message
);
writeText(null, style.error('Error:'), errorMsg);
legacy.consoleLog(chalk.yellow(' Get Support --------------------------------------------'));
legacy.consoleLog(`${chalk.yellow(' Docs: ')}docs.serverless.com`);
legacy.consoleLog(
`${chalk.yellow(' Bugs: ')}github.com/serverless/serverless/issues`
);
legacy.consoleLog(`${chalk.yellow(' Issues: ')}forum.serverless.com`);
legacy.consoleLog(' ');
legacy.consoleLog(chalk.yellow(' Your Environment Information ---------------------------'));
legacy.consoleLog(chalk.yellow(` Operating System: ${platform}`));
legacy.consoleLog(chalk.yellow(` Node Version: ${nodeVersion}`));
legacy.consoleLog(
chalk.yellow(` Framework Version: ${slsVersion}${installationModePostfix}`)
);
legacy.consoleLog(chalk.yellow(` Plugin Version: ${sfeVersion}`));
legacy.consoleLog(chalk.yellow(` SDK Version: ${platformClientVersion}`));
legacy.consoleLog(chalk.yellow(` Components Version: ${componentsVersion}`));
legacy.consoleLog(' ');
logDeprecation.printSummary();
if (
!isTelemetryDisabled &&
hasTelemetryBeenReported === false &&
(serverless ? serverless.isTelemetryReportedExternally : true)
) {
if (command == null) {
// We're in local fallback and older global didn't pass CLI command data, resolve it here
if (serverless) {
configuration = serverless.configurationInput;
serviceDir = serverless.serviceDir;
}
const commandsSchema = configuration
? require('./commands-schema/resolve-final')(serverless.pluginManager.externalPlugins, {
providerName: serverless.service.provider.name,
configuration,
})
: require('./commands-schema/aws-service');
({ command, options: cliOptions, commandSchema } = resolveInput(commandsSchema));
}
if (commandSchema) {
// Report only for recognized commands
const telemetryPayload = generateTelemetryPayload({
command,
options: cliOptions,
commandSchema,
serviceDir,
configuration,
serverless,
commandUsage,
variableSources: variableSourcesInConfig,
});
const failureReason = {
kind: isUserError ? 'user' : 'programmer',
code: exception.code,
};
if (!isUserError || !exception.code || !isErrorCodeNormative(exception.code)) {
failureReason.location = resolveErrorLocation(exceptionTokens);
}
storeTelemetryLocally({ ...telemetryPayload, failureReason, outcome: 'failure' });
await sendTelemetry();
}
}
process.exitCode = 1;
if (isUncaughtException) process.exit();
};