-
Notifications
You must be signed in to change notification settings - Fork 4
feat!: better error handling in SfCommand.catch #568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,109 @@ | ||
/* | ||
* 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(err); | ||
return new this({ | ||
message: sfError.message, | ||
name: err.name ?? 'Error', | ||
actions: 'actions' in err ? err.actions : undefined, | ||
exitCode, | ||
code: 'code' in err && err.code ? err.code : exitCode.toString(10), | ||
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, | ||
shetzel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
warnings: this.warnings, | ||
code: this.code, | ||
status: this.status, | ||
commandName: this.commandName, | ||
result: this.result, | ||
}), | ||
} as SfCommandErrorJson; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.