-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: exit codes for gacks and type errors
- Loading branch information
Showing
6 changed files
with
225 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (c) 2023, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
import { Mode, SfError, Messages, envVars } from '@salesforce/core'; | ||
import { SfCommand, StandardColors } from './sfCommand'; | ||
import { formatActions } from './formatActions'; | ||
|
||
/** | ||
* | ||
* Takes an error and returns an exit code. | ||
* Logic: | ||
* - If it looks like a gack, use that code (20) | ||
* - If it looks like a TypeError, use that code (10) | ||
* - use the exitCode if it is a number | ||
* - use the code if it is a number, or 1 if it is present not a number | ||
* - use the process exitCode | ||
* - default to 1 | ||
*/ | ||
export const computeErrorCode = (e: Error | SfError | SfCommand.Error): number => { | ||
// regardless of the exitCode, we'll set gacks and TypeError to a specific exit code | ||
if (errorIsGack(e)) { | ||
return 20; | ||
} | ||
|
||
if (errorIsTypeError(e)) { | ||
return 10; | ||
} | ||
|
||
if ('exitCode' in e && typeof e.exitCode === 'number') { | ||
return e.exitCode; | ||
} | ||
|
||
if ('code' in e) { | ||
return typeof e.code !== 'number' ? 1 : e.code; | ||
} | ||
|
||
return process.exitCode ?? 1; | ||
}; | ||
|
||
/** identifies gacks via regex. Searches the error message, stack, and recursively checks the cause chain */ | ||
export const errorIsGack = (error: Error | SfError): boolean => { | ||
/** see test for samples */ | ||
const gackRegex = /\d{9,}-\d{3,} \(-?\d{7,}\)/; | ||
return ( | ||
gackRegex.test(error.message) || | ||
(typeof error.stack === 'string' && gackRegex.test(error.stack)) || | ||
// recurse down through the error cause tree to find a gack | ||
('cause' in error && error.cause instanceof Error && errorIsGack(error.cause)) | ||
); | ||
}; | ||
|
||
/** | ||
* Format errors and actions for human consumption. Adds 'Error (<ErrorCode>):', | ||
* When there are actions, we add 'Try this:' in blue | ||
* followed by each action in red on its own line. | ||
* If Error.code is present it is output last in parentheses | ||
* | ||
* @returns {string} Returns decorated messages. | ||
*/ | ||
export const formatError = (error: SfCommand.Error): string => { | ||
Messages.importMessagesDirectory(__dirname); | ||
const messages = Messages.loadMessages('@salesforce/sf-plugins-core', 'messages'); | ||
|
||
const colorizedArgs: string[] = []; | ||
const errorCode = typeof error.code === 'string' || typeof error.code === 'number' ? ` (${error.code})` : ''; | ||
const errorPrefix = `${StandardColors.error(messages.getMessage('error.prefix', [errorCode]))}`; | ||
colorizedArgs.push(`${errorPrefix} ${error.message}`); | ||
colorizedArgs.push(...formatActions(error.actions ?? [])); | ||
if (error.stack && envVars.getString(SfCommand.SF_ENV) === Mode.DEVELOPMENT) { | ||
colorizedArgs.push(StandardColors.info(`\n*** Internal Diagnostic ***\n\n${error.stack}\n******\n`)); | ||
} | ||
return colorizedArgs.join('\n'); | ||
}; | ||
|
||
/** identifies TypeError. Searches the error message, stack, and recursively checks the cause chain */ | ||
export const errorIsTypeError = (error: Error | SfError): boolean => | ||
error instanceof TypeError || | ||
error.name === 'TypeError' || | ||
error.message.includes('TypeError') || | ||
Boolean(error.stack?.includes('TypeError')) || | ||
('cause' in error && error.cause instanceof Error && errorIsTypeError(error.cause)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright (c) 2023, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
import * as chalk from 'chalk'; | ||
import { StandardColors, messages } from './sfCommand'; | ||
|
||
export const formatActions = ( | ||
actions: string[], | ||
options: { actionColor: chalk.Chalk } = { actionColor: StandardColors.info } | ||
): string[] => { | ||
const colorizedArgs: string[] = []; | ||
// Format any actions. | ||
if (actions?.length) { | ||
colorizedArgs.push(`\n${StandardColors.info(messages.getMessage('actions.tryThis'))}\n`); | ||
actions.forEach((action) => { | ||
colorizedArgs.push(`${options.actionColor(action)}`); | ||
}); | ||
} | ||
return colorizedArgs; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) 2023, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
import { expect } from 'chai'; | ||
import { SfError } from '@salesforce/core'; | ||
import { errorIsGack, errorIsTypeError } from '../../src/errorHandling'; | ||
|
||
describe('typeErrors', () => { | ||
let typeError: Error; | ||
|
||
before(() => { | ||
try { | ||
const n = null; | ||
// @ts-expect-error I know it's wrong, I need an error! | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
n.f(); | ||
} catch (e) { | ||
if (e instanceof TypeError) { | ||
typeError = e; | ||
} | ||
} | ||
}); | ||
it('matches on TypeError as error name', () => { | ||
expect(errorIsTypeError(typeError)).to.be.true; | ||
}); | ||
|
||
it('matches on TypeError in ', () => { | ||
expect(errorIsTypeError(typeError)).to.be.true; | ||
}); | ||
|
||
it('matches on TypeError as cause', () => { | ||
const error = new SfError('some error', 'testError', [], 44, typeError); | ||
expect(errorIsTypeError(error)).to.be.true; | ||
}); | ||
}); | ||
describe('gacks', () => { | ||
const realGackSamples = [ | ||
'963190677-320016 (165202460)', | ||
'1662648879-55786 (-1856191902)', | ||
'694826414-169428 (2085174272)', | ||
'1716315817-543601 (74920438)', | ||
'1035887602-340708 (1781437152)', | ||
'671603042-121307 (-766503277)', | ||
'396937656-5973 (-766503277)', | ||
'309676439-91665 (-153174221)', | ||
'956661320-295482 (2000727581)', | ||
'1988392611-333742 (1222029414)', | ||
'1830254280-281143 (331700540)', | ||
]; | ||
|
||
it('says true for sample gacks', () => { | ||
realGackSamples.forEach((gack) => { | ||
expect(errorIsGack(new SfError(gack))).to.be.true; | ||
}); | ||
}); | ||
|
||
it('error in stack', () => { | ||
const error = new SfError('some error'); | ||
error.stack = realGackSamples[0]; | ||
expect(errorIsGack(error)).to.be.true; | ||
}); | ||
|
||
it('error in sfError cause', () => { | ||
const error = new SfError('some error', 'testError', [], 44, new Error(realGackSamples[0])); | ||
expect(errorIsGack(error)).to.be.true; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters