Skip to content

Commit 97f84c4

Browse files
committed
fix: better error handling
1 parent a49a11d commit 97f84c4

File tree

3 files changed

+111
-26
lines changed

3 files changed

+111
-26
lines changed

command-snapshot.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@
180180
"plugin": "@salesforce/plugin-packaging"
181181
},
182182
{
183-
"alias": ["force:package:pushupgrade:schedule"],
183+
"alias": [],
184184
"command": "package:pushupgrade:schedule",
185-
"flagAliases": ["apiversion", "scheduledstarttime", "target-hub-org", "targetdevhubusername"],
185+
"flagAliases": ["target-hub-org", "targetdevhubusername"],
186186
"flagChars": ["l", "p", "t", "v"],
187187
"flags": [
188188
"api-version",

schemas/package-pushupgrade-schedule.json

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema#",
3-
"$ref": "#/definitions/PackagePushScheduleResult",
3+
"$ref": "#/definitions/PushScheduleResult",
44
"definitions": {
5+
"PushScheduleResult": {
6+
"anyOf": [
7+
{
8+
"$ref": "#/definitions/PackagePushScheduleResult"
9+
},
10+
{
11+
"$ref": "#/definitions/SfError"
12+
}
13+
]
14+
},
515
"PackagePushScheduleResult": {
616
"type": "object",
717
"properties": {
@@ -17,6 +27,93 @@
1727
},
1828
"required": ["PushRequestId", "ScheduledStartTime", "Status"],
1929
"additionalProperties": false
30+
},
31+
"SfError": {
32+
"type": "object",
33+
"properties": {
34+
"name": {
35+
"type": "string"
36+
},
37+
"message": {
38+
"type": "string"
39+
},
40+
"stack": {
41+
"type": "string"
42+
},
43+
"actions": {
44+
"type": "array",
45+
"items": {
46+
"type": "string"
47+
},
48+
"description": "Action messages. Hints to the users regarding what can be done to fix related issues."
49+
},
50+
"exitCode": {
51+
"type": "number",
52+
"description": "SfdxCommand can return this process exit code."
53+
},
54+
"context": {
55+
"type": "string",
56+
"description": "The related context for this error."
57+
},
58+
"data": {
59+
"$ref": "#/definitions/AnyJson"
60+
}
61+
},
62+
"required": ["exitCode", "message", "name"],
63+
"additionalProperties": false,
64+
"description": "A generalized sfdx error which also contains an action. The action is used in the CLI to help guide users past the error.\n\nTo throw an error in a synchronous function you must either pass the error message and actions directly to the constructor, e.g.\n\n``` // To load a message bundle (Note that __dirname should contain a messages folder) Messages.importMessagesDirectory(__dirname); const messages = Messages.load('myPackageName', 'myBundleName');\n\n// To throw a non-bundle based error: throw new SfError(message.getMessage('myError'), 'MyErrorName'); ```"
65+
},
66+
"AnyJson": {
67+
"anyOf": [
68+
{
69+
"$ref": "#/definitions/JsonPrimitive"
70+
},
71+
{
72+
"$ref": "#/definitions/JsonCollection"
73+
}
74+
],
75+
"description": "Any valid JSON value."
76+
},
77+
"JsonPrimitive": {
78+
"type": ["null", "boolean", "number", "string"],
79+
"description": "Any valid JSON primitive value."
80+
},
81+
"JsonCollection": {
82+
"anyOf": [
83+
{
84+
"$ref": "#/definitions/JsonMap"
85+
},
86+
{
87+
"$ref": "#/definitions/JsonArray"
88+
}
89+
],
90+
"description": "Any valid JSON collection value."
91+
},
92+
"JsonMap": {
93+
"type": "object",
94+
"additionalProperties": {
95+
"$ref": "#/definitions/Optional%3CAnyJson%3E"
96+
},
97+
"properties": {},
98+
"description": "Any JSON-compatible object."
99+
},
100+
"Optional<AnyJson>": {
101+
"anyOf": [
102+
{
103+
"$ref": "#/definitions/AnyJson"
104+
},
105+
{
106+
"not": {}
107+
}
108+
],
109+
"description": "A union type for either the parameterized type `T` or `undefined` -- the opposite of {@link NonOptional } ."
110+
},
111+
"JsonArray": {
112+
"type": "array",
113+
"items": {
114+
"$ref": "#/definitions/AnyJson"
115+
},
116+
"description": "Any JSON-compatible array."
20117
}
21118
}
22119
}

src/commands/package/pushupgrade/schedule.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,47 @@
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77
import fs from 'node:fs/promises';
8-
import { Flags, SfCommand, orgApiVersionFlagWithDeprecations } from '@salesforce/sf-plugins-core';
8+
import { Flags, SfCommand } from '@salesforce/sf-plugins-core';
99
import { Messages, SfError } from '@salesforce/core';
1010
import { PackagePushScheduleResult, PackagePushUpgrade } from '@salesforce/packaging';
1111
import { requiredHubFlag } from '../../../utils/hubFlag.js';
1212

1313
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
1414
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_pushupgrade_schedule');
1515

16-
export class PackagePushScheduleCommand extends SfCommand<PackagePushScheduleResult | SfError> {
16+
export type PushScheduleResult = PackagePushScheduleResult | SfError;
17+
18+
export class PackagePushScheduleCommand extends SfCommand<PushScheduleResult> {
1719
public static readonly summary = messages.getMessage('summary');
1820
public static readonly description = messages.getMessage('description');
1921
public static readonly examples = messages.getMessages('examples');
2022
public static readonly hidden = true;
2123
public static state = 'beta';
22-
public static readonly aliases = ['force:package:pushupgrade:schedule'];
2324
public static readonly flags = {
2425
'target-dev-hub': requiredHubFlag,
25-
'api-version': orgApiVersionFlagWithDeprecations,
26-
'package-version-id': Flags.string({
26+
'api-version': Flags.orgApiVersion(),
27+
'package-version-id': Flags.salesforceId({
28+
length: 'both',
2729
char: 'p',
2830
summary: messages.getMessage('flags.package-version-id.summary'),
2931
required: true,
32+
startsWith: '04t',
3033
}),
3134
'scheduled-start-time': Flags.string({
3235
char: 't',
33-
deprecateAliases: true,
34-
aliases: ['scheduledstarttime'],
3536
summary: messages.getMessage('flags.scheduled-start-time.summary'),
3637
}),
3738
'org-list': Flags.file({
3839
char: 'l',
3940
summary: messages.getMessage('flags.org-list.summary'),
4041
required: true,
42+
exists: true,
4143
}),
4244
};
4345

44-
public async run(): Promise<PackagePushScheduleResult | SfError> {
46+
public async run(): Promise<PushScheduleResult> {
4547
const { flags } = await this.parse(PackagePushScheduleCommand);
4648

47-
// Validate package version
48-
if (!isValidPackageVersionId(flags['package-version-id'])) {
49-
throw new SfError(messages.getMessage('error.invalid-package-version'));
50-
}
51-
5249
// Read and validate org list
5350
const orgList = await readOrgListFile(flags['org-list']);
5451

@@ -73,22 +70,13 @@ export class PackagePushScheduleCommand extends SfCommand<PackagePushScheduleRes
7370
}
7471
}
7572

76-
function isValidPackageVersionId(id: string): boolean {
77-
// Implement validation logic for 04t id
78-
return /^04t[a-zA-Z0-9]{15}$/.test(id);
79-
}
80-
8173
async function readOrgListFile(filePath: string): Promise<string[]> {
8274
try {
8375
const fileContent = await fs.readFile(filePath, 'utf-8');
8476
const orgIds = fileContent.split(/\s+/).filter((id) => id.length > 0);
8577

86-
if (orgIds.length === 0) {
87-
throw new SfError(messages.getMessage('error.empty-org-list'));
88-
}
89-
9078
return orgIds.filter((id: string) => /^00D[a-zA-Z0-9]{12}$/.test(id));
9179
} catch (error) {
92-
throw new SfError(messages.getMessage('error.invalid-org-list-file'));
80+
throw new SfError(messages.getMessage('error.invalid-org-list-file'), error as string | undefined);
9381
}
9482
}

0 commit comments

Comments
 (0)