Skip to content

Commit

Permalink
fix: error message for snapshot create (#532)
Browse files Browse the repository at this point in the history
* fix: error message for snapshot create

* fix: update messages/snapshot.md

Co-authored-by: Juliet Shackell <[email protected]>

---------

Co-authored-by: Juliet Shackell <[email protected]>
  • Loading branch information
agomez-sf and jshackell-sfdc authored Mar 29, 2024
1 parent b2c7514 commit 33d2c83
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion messages/snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ No snapshot found with the given name or id: %s

# snapshotNotEnabled

Org snapshots aren’t enabled for your Dev Hub.
Scratch Org Snapshots isn't enabled for your Dev Hub.
32 changes: 25 additions & 7 deletions src/commands/org/create/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ import {
requiredHubFlagWithDeprecations,
} from '@salesforce/sf-plugins-core';
import { StateAggregator, Messages, SfError } from '@salesforce/core';
import { OrgSnapshot, queryByNameOrId, printSingleRecordTable } from '../../../shared/snapshot.js';
import {
OrgSnapshot,
queryByNameOrId,
printSingleRecordTable,
invalidTypeErrorHandler,
} from '../../../shared/snapshot.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-signups', 'snapshot.create');
const snapshotMessages = Messages.loadMessages('@salesforce/plugin-signups', 'snapshot');

export class SnapshotCreate extends SfCommand<OrgSnapshot> {
public static readonly summary = messages.getMessage('summary');
Expand Down Expand Up @@ -57,12 +63,24 @@ export class SnapshotCreate extends SfCommand<OrgSnapshot> {
const { flags } = await this.parse(SnapshotCreate);

const conn = flags['target-dev-hub'].getConnection(flags['api-version']);
const createResponse = await conn.sobject('OrgSnapshot').create({
SourceOrg: flags['source-org'],
Description: flags.description,
SnapshotName: flags.name,
Content: 'metadatadata',
});
const createResponse = await conn
.sobject('OrgSnapshot')
.create({
SourceOrg: flags['source-org'],
Description: flags.description,
SnapshotName: flags.name,
Content: 'metadatadata',
})
.catch((error: Error) => {
// dev hub does not have snapshot pref enabled
if (error.name === 'NOT_FOUND') {
error.message = snapshotMessages.getMessage('snapshotNotEnabled');
return invalidTypeErrorHandler(error);
} else {
throw error;
}
});

if (createResponse.success === false) {
throw new SfError('An error while created the org snapshot');
}
Expand Down
4 changes: 2 additions & 2 deletions src/shared/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Connection, SfError, Messages } from '@salesforce/core';
import { capitalCase } from 'change-case';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-signups', 'snapshot');
export const messages = Messages.loadMessages('@salesforce/plugin-signups', 'snapshot');

export interface OrgSnapshotRequest {
SourceOrg: string;
Expand Down Expand Up @@ -71,7 +71,7 @@ const ORG_SNAPSHOT_COLUMNS = {
},
};

const invalidTypeErrorHandler = (e: unknown): never => {
export const invalidTypeErrorHandler = (e: unknown): never => {
if (e instanceof Error && e.name === 'INVALID_TYPE') {
e.message = messages.getMessage('snapshotNotEnabled');
}
Expand Down

0 comments on commit 33d2c83

Please sign in to comment.