mirror of
https://github.com/serverless/serverless.git
synced 2025-12-08 19:46:03 +00:00
hanlding errors in error handling logic
This commit is contained in:
parent
5821a5b474
commit
71172545e5
@ -11,62 +11,66 @@ module.exports.SError = class ServerlessError extends Error {
|
||||
};
|
||||
|
||||
module.exports.logError = (e) => {
|
||||
const consoleLog = (message) => {
|
||||
console.log(message); // eslint-disable-line no-console
|
||||
};
|
||||
try {
|
||||
const consoleLog = (message) => {
|
||||
console.log(message); // eslint-disable-line no-console
|
||||
};
|
||||
|
||||
const errorType = e.name.replace(/([A-Z])/g, ' $1');
|
||||
let line = '';
|
||||
while (line.length < 56 - errorType.length) {
|
||||
line = `${line}-`;
|
||||
}
|
||||
|
||||
consoleLog(' ');
|
||||
consoleLog(chalk.yellow(` ${errorType} ${line}`));
|
||||
consoleLog(' ');
|
||||
|
||||
// format error message to fit neatly
|
||||
const words = e.message.split(' ');
|
||||
let logLine = [];
|
||||
words.forEach(word => {
|
||||
logLine.push(word);
|
||||
const logLineString = logLine.join(' ');
|
||||
if (logLineString.length > 50) {
|
||||
consoleLog(chalk.yellow(` ${logLineString}`));
|
||||
logLine = [];
|
||||
const errorType = e.name.replace(/([A-Z])/g, ' $1');
|
||||
let line = '';
|
||||
while (line.length < 56 - errorType.length) {
|
||||
line = `${line}-`;
|
||||
}
|
||||
});
|
||||
|
||||
if (logLine.length !== 0) {
|
||||
consoleLog(chalk.yellow(` ${logLine.join(' ')}`));
|
||||
}
|
||||
|
||||
consoleLog(' ');
|
||||
|
||||
if (e.name !== 'ServerlessError') {
|
||||
consoleLog(chalk.red(' For debugging logs, run again after setting SLS_DEBUG env var.'));
|
||||
consoleLog(' ');
|
||||
}
|
||||
|
||||
if (process.env.SLS_DEBUG) {
|
||||
consoleLog(chalk.yellow(' Stack Trace --------------------------------------------'));
|
||||
consoleLog(chalk.yellow(` ${errorType} ${line}`));
|
||||
consoleLog(' ');
|
||||
consoleLog(e.stack);
|
||||
|
||||
// format error message to fit neatly
|
||||
const words = e.message.split(' ');
|
||||
let logLine = [];
|
||||
words.forEach(word => {
|
||||
logLine.push(word);
|
||||
const logLineString = logLine.join(' ');
|
||||
if (logLineString.length > 50) {
|
||||
consoleLog(chalk.yellow(` ${logLineString}`));
|
||||
logLine = [];
|
||||
}
|
||||
});
|
||||
|
||||
if (logLine.length !== 0) {
|
||||
consoleLog(chalk.yellow(` ${logLine.join(' ')}`));
|
||||
}
|
||||
|
||||
consoleLog(' ');
|
||||
}
|
||||
|
||||
consoleLog(chalk.yellow(' Get Support --------------------------------------------'));
|
||||
consoleLog(`${chalk.yellow(' Docs: ')}${chalk.white('v1.docs.serverless.com')}`);
|
||||
consoleLog(`${chalk.yellow(' Bugs: ')}${chalk
|
||||
.white('github.com/serverless/serverless/issues')}`);
|
||||
if (e.name !== 'ServerlessError') {
|
||||
consoleLog(chalk.red(' For debugging logs, run again after setting SLS_DEBUG env var.'));
|
||||
consoleLog(' ');
|
||||
}
|
||||
|
||||
if (process.env.SLS_DEBUG) {
|
||||
consoleLog(chalk.yellow(' Stack Trace --------------------------------------------'));
|
||||
consoleLog(' ');
|
||||
consoleLog(e.stack);
|
||||
consoleLog(' ');
|
||||
}
|
||||
|
||||
consoleLog(chalk.yellow(' Get Support --------------------------------------------'));
|
||||
consoleLog(`${chalk.yellow(' Docs: ')}${chalk.white('v1.docs.serverless.com')}`);
|
||||
consoleLog(`${chalk.yellow(' Bugs: ')}${chalk
|
||||
.white('github.com/serverless/serverless/issues')}`);
|
||||
|
||||
if (e.name !== 'ServerlessError') {
|
||||
consoleLog(' ');
|
||||
consoleLog(chalk.red(' Please report this error. We think it might be a bug.'));
|
||||
}
|
||||
|
||||
if (e.name !== 'ServerlessError') {
|
||||
consoleLog(' ');
|
||||
consoleLog(chalk.red(' Please report this error. We think it might be a bug.'));
|
||||
|
||||
// Failure exit
|
||||
process.exit(1);
|
||||
} catch (errorHandlingError) {
|
||||
throw new Error(e);
|
||||
}
|
||||
|
||||
consoleLog(' ');
|
||||
|
||||
// Failure exit
|
||||
process.exit(1);
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user