-
Notifications
You must be signed in to change notification settings - Fork 69
/
scratchOrgErrorCodes.ts
98 lines (91 loc) · 3.6 KB
/
scratchOrgErrorCodes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Copyright (c) 2021, 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 { Optional } from '@salesforce/ts-types';
import { Messages } from '../messages';
import { SfError } from '../sfError';
import { Logger } from '../logger/logger';
import { ScratchOrgInfo } from './scratchOrgTypes';
import { ScratchOrgCache } from './scratchOrgCache';
import { emit } from './scratchOrgLifecycleEvents';
const WORKSPACE_CONFIG_FILENAME = 'sfdx-project.json';
Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/core', 'scratchOrgErrorCodes');
const namedMessages = Messages.loadMessages('@salesforce/core', 'scratchOrgErrorCodes');
// getMessage will throw when the code isn't found
// and we don't know whether a given code takes arguments or not
const optionalErrorCodeMessage = (errorCode: string, args: string[]): string | undefined => {
try {
// only apply args if message requires them
let message = messages.getMessage(errorCode);
if (message.includes('%s')) {
message = messages.getMessage(errorCode, args);
}
return message;
} catch {
// generic error message
return undefined;
}
};
export const validateScratchOrgInfoForResume = async ({
jobId,
scratchOrgInfo,
cache,
hubUsername,
}: {
jobId: string;
scratchOrgInfo: ScratchOrgInfo;
cache: ScratchOrgCache;
hubUsername: string;
}): Promise<ScratchOrgInfo> => {
if (!scratchOrgInfo?.Id || scratchOrgInfo.Status === 'Deleted') {
// 1. scratch org info does not exist in that dev hub or has been deleted
cache.unset(jobId);
await cache.write();
throw scratchOrgInfo.Status === 'Deleted'
? namedMessages.createError('ScratchOrgDeletedError')
: namedMessages.createError('NoScratchOrgInfoError');
}
if (['New', 'Creating'].includes(scratchOrgInfo.Status)) {
// 2. scratchOrgInfo exists, still isn't finished. Stays in cache for future attempts
throw namedMessages.createError('StillInProgressError', [scratchOrgInfo.Status], ['action.StillInProgress']);
}
return checkScratchOrgInfoForErrors(scratchOrgInfo, hubUsername);
};
export const checkScratchOrgInfoForErrors = async (
orgInfo: Optional<ScratchOrgInfo>,
hubUsername: Optional<string>
): Promise<ScratchOrgInfo> => {
if (!orgInfo?.Id) {
throw new SfError('No scratch org info found.', 'ScratchOrgInfoNotFound');
}
if (orgInfo.Status === 'Active') {
await emit({ stage: 'available', scratchOrgInfo: orgInfo });
return orgInfo;
}
if (orgInfo.Status === 'Error' && orgInfo.ErrorCode) {
await ScratchOrgCache.unset(orgInfo.Id);
const message = optionalErrorCodeMessage(orgInfo.ErrorCode, [WORKSPACE_CONFIG_FILENAME]);
if (message) {
throw new SfError(message, 'RemoteOrgSignupFailed', [
namedMessages.getMessage('SignupFailedActionError', [orgInfo.ErrorCode]),
]);
}
throw new SfError(namedMessages.getMessage('SignupFailedError', [orgInfo.ErrorCode]));
}
if (orgInfo.Status === 'Error') {
await ScratchOrgCache.unset(orgInfo.Id);
const logger = await Logger.child('ScratchOrgErrorCodes');
// Maybe the request object can help the user somehow
logger.error('No error code on signup error! Logging request.');
logger.error(orgInfo);
throw new SfError(
namedMessages.getMessage('SignupFailedUnknownError', [orgInfo.Id, hubUsername]),
'signupFailedUnknown'
);
}
throw new SfError(namedMessages.getMessage('SignupUnexpectedError'), 'UnexpectedSignupStatus');
};