-
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.
Loading status checks…
feat: better error handling in SfCommand.catch
- 12.2.0
- 12.1.4
- 12.1.3
- 12.1.2
- 12.1.1
- 12.1.0
- 12.0.13
- 12.0.12
- 12.0.11
- 12.0.10
- 12.0.9
- 12.0.8
- 12.0.7
- 12.0.6
- 12.0.5
- 12.0.4
- 12.0.3
- 12.0.2
- 12.0.1
- 12.0.0
- 11.3.12
- 11.3.11
- 11.3.10
- 11.3.9
- 11.3.8
- 11.3.7
- 11.3.6
- 11.3.5
- 11.3.4
- 11.3.3
- 11.3.3-dev.3
- 11.3.3-dev.2
- 11.3.3-dev.1
- 11.3.3-dev.0
- 11.3.2
- 11.3.1
- 11.3.0
- 11.2.8
- 11.2.7
- 11.2.6
- 11.2.5
- 11.2.4
- 11.2.3
- 11.2.2
- 11.2.1
- 11.2.0
- 11.1.9
- 11.1.8
- 11.1.7
- 11.1.6
- 11.1.5
- 11.1.4
- 11.1.3
- 11.1.2
- 11.1.1
- 11.1.0
Showing
8 changed files
with
578 additions
and
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* 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 { SfError, StructuredMessage } from '@salesforce/core'; | ||
import { AnyJson } from '@salesforce/ts-types'; | ||
import { computeErrorCode } from './errorHandling.js'; | ||
import { removeEmpty } from './util.js'; | ||
|
||
// These types are 90% the same as SfErrorOptions (but they aren't exported to extend) | ||
type ErrorDataProperties = AnyJson; | ||
export type SfCommandErrorOptions<T extends ErrorDataProperties = ErrorDataProperties> = { | ||
message: string; | ||
exitCode: number; | ||
code: string; | ||
name?: string; | ||
commandName: string; | ||
data?: T; | ||
cause?: unknown; | ||
context?: string; | ||
actions?: string[]; | ||
result?: unknown; | ||
warnings?: Array<StructuredMessage | string>; | ||
}; | ||
|
||
type SfCommandErrorJson = { | ||
name: string; | ||
message: string; | ||
exitCode: number; | ||
commandName: string; | ||
context: string; | ||
code: string; | ||
status: string; | ||
stack: string; | ||
actions?: string; | ||
data?: ErrorDataProperties; | ||
cause?: string; | ||
warnings?: Array<StructuredMessage | string>; | ||
result?: unknown; | ||
}; | ||
|
||
export class SfCommandError extends SfError { | ||
public status: number; | ||
public commandName: string; | ||
public warnings?: Array<StructuredMessage | string>; | ||
public result?: unknown; | ||
public skipOclifErrorHandling: boolean; | ||
public oclif: { exit: number }; | ||
|
||
/** | ||
* SfCommandError is meant to wrap errors from `SfCommand.catch()` for a common | ||
* error format to be logged, sent to telemetry, and re-thrown. Do not create | ||
* instances from the constructor. Call the static method, `SfCommandError.from()` | ||
* and use the returned `SfCommandError`. | ||
*/ | ||
private constructor(input: SfCommandErrorOptions) { | ||
super(input.message, input.name, input.actions, input.exitCode, input.cause); | ||
this.data = input.data; | ||
this.status = input.exitCode; | ||
this.warnings = input.warnings; | ||
this.skipOclifErrorHandling = true; | ||
this.commandName = input.commandName; | ||
this.code = input.code; | ||
this.result = input.result; | ||
this.oclif = { exit: input.exitCode }; | ||
this.context = input.context ?? input.commandName; | ||
} | ||
|
||
public static from( | ||
err: Error | SfError | SfCommandError, | ||
commandName: string, | ||
warnings?: Array<StructuredMessage | string> | ||
): SfCommandError { | ||
// SfError.wrap() does most of what we want so start with that. | ||
const sfError = SfError.wrap(err); | ||
const exitCode = computeErrorCode(sfError); | ||
sfError.code = 'code' in err ? err.code : exitCode.toString(10); | ||
return new this({ | ||
message: sfError.message, | ||
name: err.name ?? 'Error', | ||
actions: 'actions' in err ? err.actions : undefined, | ||
exitCode, | ||
code: sfError.code, | ||
cause: sfError.cause, | ||
commandName: 'commandName' in err ? err.commandName : commandName, | ||
data: 'data' in err ? err.data : undefined, | ||
result: 'result' in err ? err.result : undefined, | ||
context: 'context' in err ? err.context : commandName, | ||
warnings, | ||
}); | ||
} | ||
|
||
public toJson(): SfCommandErrorJson { | ||
return { | ||
...removeEmpty({ | ||
// toObject() returns name, message, exitCode, actions, context, data | ||
...this.toObject(), | ||
stack: this.stack, | ||
cause: this.cause, | ||
warnings: this.warnings, | ||
code: this.code, | ||
status: this.status, | ||
commandName: this.commandName, | ||
result: this.result, | ||
}), | ||
} as SfCommandErrorJson; | ||
} | ||
} |
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
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 was deleted.
Oops, something went wrong.
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,82 @@ | ||
/* | ||
* 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 { Mode, SfError } from '@salesforce/core'; | ||
import { formatError } from '../../src/errorFormatting.js'; | ||
import { SfCommandError } from '../../src/SfCommandError.js'; | ||
|
||
describe('errorFormatting.formatError()', () => { | ||
afterEach(() => { | ||
delete process.env.SF_ENV; | ||
}); | ||
|
||
it('should have correct output for non-development mode, no actions', () => { | ||
const err = SfCommandError.from(new Error('this did not work'), 'thecommand'); | ||
const errorOutput = formatError(err); | ||
expect(errorOutput).to.contain('Error (1)'); | ||
expect(errorOutput).to.contain('this did not work'); | ||
}); | ||
|
||
it('should have correct output for non-development mode, with actions', () => { | ||
const sfError = new SfError('this did not work', 'BadError'); | ||
const err = SfCommandError.from(sfError, 'thecommand'); | ||
err.actions = ['action1', 'action2']; | ||
const errorOutput = formatError(err); | ||
expect(errorOutput).to.contain('Error (BadError)'); | ||
expect(errorOutput).to.contain('this did not work'); | ||
expect(errorOutput).to.contain('Try this:'); | ||
expect(errorOutput).to.contain('action1'); | ||
expect(errorOutput).to.contain('action2'); | ||
}); | ||
|
||
it('should have correct output for development mode, basic error', () => { | ||
process.env.SF_ENV = Mode.DEVELOPMENT; | ||
const err = SfCommandError.from(new SfError('this did not work'), 'thecommand'); | ||
const errorOutput = formatError(err); | ||
expect(errorOutput).to.contain('Error (SfError)'); | ||
expect(errorOutput).to.contain('this did not work'); | ||
expect(errorOutput).to.contain('*** Internal Diagnostic ***'); | ||
expect(errorOutput).to.contain('at Function.from'); | ||
expect(errorOutput).to.contain('actions: undefined'); | ||
expect(errorOutput).to.contain('exitCode: 1'); | ||
expect(errorOutput).to.contain("context: 'thecommand'"); | ||
expect(errorOutput).to.contain('data: undefined'); | ||
expect(errorOutput).to.contain('cause: undefined'); | ||
expect(errorOutput).to.contain('status: 1'); | ||
expect(errorOutput).to.contain("commandName: 'thecommand'"); | ||
expect(errorOutput).to.contain('warnings: undefined'); | ||
expect(errorOutput).to.contain('result: undefined'); | ||
}); | ||
|
||
it('should have correct output for development mode, full error', () => { | ||
process.env.SF_ENV = Mode.DEVELOPMENT; | ||
const sfError = SfError.create({ | ||
name: 'WOMP_WOMP', | ||
message: 'this did not work', | ||
actions: ['action1', 'action2'], | ||
cause: new Error('this is the cause'), | ||
exitCode: 9, | ||
context: 'somecommand', | ||
data: { foo: 'bar' }, | ||
}); | ||
const err = SfCommandError.from(sfError, 'thecommand'); | ||
const errorOutput = formatError(err); | ||
expect(errorOutput).to.contain('Error (WOMP_WOMP)'); | ||
expect(errorOutput).to.contain('this did not work'); | ||
expect(errorOutput).to.contain('*** Internal Diagnostic ***'); | ||
expect(errorOutput).to.contain('at Function.from'); | ||
expect(errorOutput).to.contain("actions: [ 'action1', 'action2' ]"); | ||
expect(errorOutput).to.contain('exitCode: 9'); | ||
expect(errorOutput).to.contain("context: 'somecommand'"); | ||
expect(errorOutput).to.contain("data: { foo: 'bar' }"); | ||
expect(errorOutput).to.contain('cause: Error: this is the cause'); | ||
expect(errorOutput).to.contain('status: 9'); | ||
expect(errorOutput).to.contain("commandName: 'thecommand'"); | ||
expect(errorOutput).to.contain('warnings: undefined'); | ||
expect(errorOutput).to.contain('result: undefined'); | ||
}); | ||
}); |
Oops, something went wrong.