diff --git a/messages/snapshot.md b/messages/snapshot.md index c090a97d..83327a28 100644 --- a/messages/snapshot.md +++ b/messages/snapshot.md @@ -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. diff --git a/src/commands/org/create/snapshot.ts b/src/commands/org/create/snapshot.ts index 070d6511..5c82aadf 100644 --- a/src/commands/org/create/snapshot.ts +++ b/src/commands/org/create/snapshot.ts @@ -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 { public static readonly summary = messages.getMessage('summary'); @@ -57,12 +63,24 @@ export class SnapshotCreate extends SfCommand { 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'); } diff --git a/src/shared/snapshot.ts b/src/shared/snapshot.ts index 426eb5fc..999ecf21 100644 --- a/src/shared/snapshot.ts +++ b/src/shared/snapshot.ts @@ -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; @@ -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'); }