Skip to content

Commit

Permalink
refactor: typing for sfError.getObject, omit undefined props
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Apr 9, 2024
1 parent c0c1283 commit d104d0a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
27 changes: 21 additions & 6 deletions src/sfError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { AnyJson, hasString, isString, JsonMap } from '@salesforce/ts-types';
import { AnyJson, hasString, isString } from '@salesforce/ts-types';

export type SfErrorOptions<T extends ErrorDataProperties = ErrorDataProperties> = {
message: string;
Expand All @@ -20,6 +20,15 @@ export type SfErrorOptions<T extends ErrorDataProperties = ErrorDataProperties>

type ErrorDataProperties = AnyJson;

type SfErrorToObjectResult = {
name: string;
message: string;
exitCode: number;
actions?: string[];
context?: string;
data?: ErrorDataProperties;
};

/**
* A generalized sfdx error which also contains an action. The action is used in the
* CLI to help guide users past the error.
Expand Down Expand Up @@ -83,7 +92,9 @@ export class SfError<T extends ErrorDataProperties = ErrorDataProperties> extend
super(message);
this.name = name;
this.cause = exitCodeOrCause instanceof Error ? exitCodeOrCause : cause;
this.actions = actions;
if (actions?.length) {
this.actions = actions;
}
if (typeof exitCodeOrCause === 'number') {
this.exitCode = exitCodeOrCause;
} else {
Expand All @@ -102,8 +113,12 @@ export class SfError<T extends ErrorDataProperties = ErrorDataProperties> extend
/** like the constructor, but takes an typed object and let you also set context and data properties */
public static create<T extends ErrorDataProperties = ErrorDataProperties>(inputs: SfErrorOptions<T>): SfError<T> {
const error = new SfError<T>(inputs.message, inputs.name, inputs.actions, inputs.exitCode, inputs.cause);
error.data = inputs.data;
error.context = inputs.context;
if (inputs.data) {
error.data = inputs.data;
}
if (inputs.context) {
error.context = inputs.context;
}
return error;
}
/**
Expand Down Expand Up @@ -164,12 +179,12 @@ export class SfError<T extends ErrorDataProperties = ErrorDataProperties> extend
/**
* Convert an {@link SfError} state to an object. Returns a plain object representing the state of this error.
*/
public toObject(): JsonMap {
public toObject(): SfErrorToObjectResult {
return {
name: this.name,
message: this.message ?? this.name,
exitCode: this.exitCode,
actions: this.actions,
...(this.actions?.length ? { actions: this.actions } : {}),
...(this.context ? { context: this.context } : {}),
...(this.data ? { data: this.data } : {}),
};
Expand Down
1 change: 0 additions & 1 deletion test/unit/sfErrorTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ describe('SfError', () => {
name,
message,
exitCode: 1,
actions: undefined,
});
});
});
Expand Down

2 comments on commit d104d0a

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - ubuntu-latest

Benchmark suite Current: d104d0a Previous: 54865d7 Ratio
Child logger creation 472758 ops/sec (±0.42%) 478190 ops/sec (±0.48%) 1.01
Logging a string on root logger 823520 ops/sec (±8.92%) 790854 ops/sec (±7.84%) 0.96
Logging an object on root logger 569756 ops/sec (±6.85%) 570203 ops/sec (±6.97%) 1.00
Logging an object with a message on root logger 7338 ops/sec (±210.97%) 10960 ops/sec (±200.47%) 1.49
Logging an object with a redacted prop on root logger 430264 ops/sec (±7.52%) 405374 ops/sec (±17.81%) 0.94
Logging a nested 3-level object on root logger 361798 ops/sec (±8.22%) 367034 ops/sec (±10.08%) 1.01

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - windows-latest

Benchmark suite Current: d104d0a Previous: 54865d7 Ratio
Child logger creation 319458 ops/sec (±0.92%) 306458 ops/sec (±3.41%) 0.96
Logging a string on root logger 759703 ops/sec (±6.42%) 719839 ops/sec (±6.07%) 0.95
Logging an object on root logger 609842 ops/sec (±9.96%) 588030 ops/sec (±6.44%) 0.96
Logging an object with a message on root logger 8317 ops/sec (±202.37%) 9518 ops/sec (±198.54%) 1.14
Logging an object with a redacted prop on root logger 448039 ops/sec (±7.26%) 423435 ops/sec (±10.96%) 0.95
Logging a nested 3-level object on root logger 325566 ops/sec (±5.67%) 326534 ops/sec (±4.88%) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.