UNPKG

4.3 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright (c) 2021, salesforce.com, inc.
4 * All rights reserved.
5 * Licensed under the BSD 3-Clause license.
6 * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
7 */
8Object.defineProperty(exports, "__esModule", { value: true });
9exports.checkScratchOrgInfoForErrors = exports.validateScratchOrgInfoForResume = void 0;
10const messages_1 = require("../messages");
11const sfError_1 = require("../sfError");
12const logger_1 = require("../logger");
13const scratchOrgCache_1 = require("./scratchOrgCache");
14const scratchOrgLifecycleEvents_1 = require("./scratchOrgLifecycleEvents");
15const WORKSPACE_CONFIG_FILENAME = 'sfdx-project.json';
16messages_1.Messages.importMessagesDirectory(__dirname);
17const messages = messages_1.Messages.loadMessages('@salesforce/core', 'scratchOrgErrorCodes');
18const namedMessages = messages_1.Messages.load('@salesforce/core', 'scratchOrgErrorCodes', [
19 'SignupFailedActionError',
20 'SignupFailedUnknownError',
21 'SignupFailedError',
22 'SignupUnexpectedError',
23 'StillInProgressError',
24 'action.StillInProgress',
25 'ScratchOrgDeletedError',
26 'NoScratchOrgInfoError',
27]);
28// getMessage will throw when the code isn't found
29// and we don't know whether a given code takes arguments or not
30const optionalErrorCodeMessage = (errorCode, args) => {
31 try {
32 // only apply args if message requires them
33 let message = messages.getMessage(errorCode);
34 if (message.includes('%s')) {
35 message = messages.getMessage(errorCode, args);
36 }
37 return message;
38 }
39 catch {
40 // generic error message
41 return undefined;
42 }
43};
44const validateScratchOrgInfoForResume = async ({ jobId, scratchOrgInfo, cache, hubUsername, }) => {
45 if (!scratchOrgInfo || !scratchOrgInfo.Id || scratchOrgInfo.Status === 'Deleted') {
46 // 1. scratch org info does not exist in that dev hub or has been deleted
47 cache.unset(jobId);
48 await cache.write();
49 throw scratchOrgInfo.Status === 'Deleted'
50 ? namedMessages.createError('ScratchOrgDeletedError')
51 : namedMessages.createError('NoScratchOrgInfoError');
52 }
53 if (['New', 'Creating'].includes(scratchOrgInfo.Status)) {
54 // 2. scratchOrgInfo exists, still isn't finished. Stays in cache for future attempts
55 throw namedMessages.createError('StillInProgressError', [scratchOrgInfo.Status], ['action.StillInProgress']);
56 }
57 return (0, exports.checkScratchOrgInfoForErrors)(scratchOrgInfo, hubUsername);
58};
59exports.validateScratchOrgInfoForResume = validateScratchOrgInfoForResume;
60const checkScratchOrgInfoForErrors = async (orgInfo, hubUsername) => {
61 if (!orgInfo || !orgInfo.Id) {
62 throw new sfError_1.SfError('No scratch org info found.', 'ScratchOrgInfoNotFound');
63 }
64 if (orgInfo.Status === 'Active') {
65 await (0, scratchOrgLifecycleEvents_1.emit)({ stage: 'available', scratchOrgInfo: orgInfo });
66 return orgInfo;
67 }
68 if (orgInfo.Status === 'Error' && orgInfo.ErrorCode) {
69 await scratchOrgCache_1.ScratchOrgCache.unset(orgInfo.Id);
70 const message = optionalErrorCodeMessage(orgInfo.ErrorCode, [WORKSPACE_CONFIG_FILENAME]);
71 if (message) {
72 throw new sfError_1.SfError(message, 'RemoteOrgSignupFailed', [
73 namedMessages.getMessage('SignupFailedActionError', [orgInfo.ErrorCode]),
74 ]);
75 }
76 throw new sfError_1.SfError(namedMessages.getMessage('SignupFailedError', [orgInfo.ErrorCode]));
77 }
78 if (orgInfo.Status === 'Error') {
79 await scratchOrgCache_1.ScratchOrgCache.unset(orgInfo.Id);
80 const logger = await logger_1.Logger.child('ScratchOrgErrorCodes');
81 // Maybe the request object can help the user somehow
82 logger.error('No error code on signup error! Logging request.');
83 logger.error(orgInfo);
84 throw new sfError_1.SfError(namedMessages.getMessage('SignupFailedUnknownError', [orgInfo.Id, hubUsername]), 'signupFailedUnknown');
85 }
86 throw new sfError_1.SfError(namedMessages.getMessage('SignupUnexpectedError'), 'UnexpectedSignupStatus');
87};
88exports.checkScratchOrgInfoForErrors = checkScratchOrgInfoForErrors;
89//# sourceMappingURL=scratchOrgErrorCodes.js.map
\No newline at end of file