UNPKG

5.37 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright, 1999-2016, salesforce.com
4 * All Rights Reserved
5 * Company Confidential
6 */
7Object.defineProperty(exports, "__esModule", { value: true });
8const _ = require("lodash");
9const almError = require("./core/almError");
10const defaultConnectedApp = require('./core/defaultConnectedApp');
11const Messages = require("./messages");
12const messages = Messages();
13const BUNDLE_NAME = 'IndexErrorProcessor';
14/**
15 * Simple enum so the error processors can returns something that indicates the check
16 * completed and no problems were found.
17 */
18var CheckStatus;
19(function (CheckStatus) {
20 CheckStatus[CheckStatus["OK"] = 0] = "OK";
21})(CheckStatus = exports.CheckStatus || (exports.CheckStatus = {}));
22/**
23 * Returns an array of processors used to determine if an error can be further refined. Instead of
24 * adding more error handing logic to index.js add it here, as it's much easier to unit test.
25 * @param appConfig - the sfdx configuration
26 * @param context - the cli context
27 * @param err - a potentially course grained error thrown by the cli.
28 */
29function getProcessors(appConfig, context, err) {
30 return [
31 checkVersionMisMatchAsync(context, err),
32 checkServer500(err),
33 checkOauthAnd404(appConfig, context, err),
34 checkInvalidLoginUrlWithAccessToken(context, err)
35 ];
36}
37exports.getProcessors = getProcessors;
38/**
39 * Check is there is an invalid grant with oauth or a 404 response from the server.
40 * @param appConfig - sfdx configuration
41 * @param context - cli context
42 * @param err - an error from the cli
43 */
44function checkOauthAnd404(appConfig, context, err) {
45 if (context && err && (err.name === 'invalid_grant' || err.name === 'ERROR_HTTP_404')) {
46 const notFoundMessage = messages.getMessage('notSpecified');
47 let authConfig = {};
48 if (context.org) {
49 authConfig = context.org.authConfig;
50 }
51 else {
52 _.set(authConfig, 'username', context.flags.username);
53 _.set(authConfig, 'clientId', context.flags.clientid);
54 _.set(authConfig, 'privateKey', context.flags.jwtkeyfile);
55 if (appConfig) {
56 _.set(authConfig, 'loginUrl', appConfig.sfdcLoginUrl);
57 }
58 }
59 throw almError('oauthInvalidGrant', [
60 // We know the 404 and invalid grant error always contain a name and message.
61 // The 404 error message is an html error page response.
62 err.name.includes('404') ? err.name : `${err.name} - ${err.message}`,
63 _.isNil(authConfig.username) ? notFoundMessage : authConfig.username,
64 _.isNil(authConfig.clientId) || authConfig.clientId === defaultConnectedApp.legacyClientId
65 ? notFoundMessage
66 : authConfig.clientId,
67 _.isNil(authConfig.loginUrl) ? notFoundMessage : authConfig.loginUrl,
68 _.isNil(authConfig.privateKey) ? notFoundMessage : authConfig.privateKey
69 ], 'oauthInvalidGrantAction');
70 }
71 return CheckStatus.OK;
72}
73exports.checkOauthAnd404 = checkOauthAnd404;
74/**
75 * Check that the servers api version is <= to the local config apiVersion.
76 * @param context - the cli context that contains an org
77 * @param _err - an error thrown by the cli
78 */
79async function checkVersionMisMatchAsync(context, _err) {
80 if (_err && _err.name === 'NOT_FOUND') {
81 if (context && context.org) {
82 const maxApiVersionForOrg = await context.org.retrieveMaxApiVersion();
83 const configVersion = context.org.force.config.getApiVersion();
84 if (_.toNumber(configVersion) > _.toNumber(maxApiVersionForOrg.version)) {
85 throw almError({ bundle: BUNDLE_NAME, keyName: 'apiMisMatch' }, [configVersion, maxApiVersionForOrg.version], {
86 keyName: 'apiMisMatchAction',
87 bundle: BUNDLE_NAME
88 });
89 }
90 }
91 }
92 return CheckStatus.OK;
93}
94exports.checkVersionMisMatchAsync = checkVersionMisMatchAsync;
95/**
96 * Check to see if the throw error is a server 500. THis error is critical. If a database is being update in production
97 * This error is throw after a rest style connection. It's imperative that customer's get a link to http://trust.salesforce.com
98 * @param _err - an error to process thrown by the cli.
99 */
100function checkServer500(_err) {
101 if (_err && _err.name === 'ERROR_HTTP_500' && _.isEmpty(_.trim(_err.message))) {
102 throw almError({ bundle: BUNDLE_NAME, keyName: 'server500' }, null, {
103 bundle: BUNDLE_NAME,
104 keyName: 'server500Action'
105 });
106 }
107 return CheckStatus.OK;
108}
109exports.checkServer500 = checkServer500;
110function checkInvalidLoginUrlWithAccessToken(context, err) {
111 // provide action if instanceurl is incorrect
112 if (context.org &&
113 context.org.usingAccessToken &&
114 (err.message.match(/Session expired or invalid/) || err.message.match(/Destination URL not reset/))) {
115 err['message'] = messages.getMessage('accessTokenLoginUrlNotSet', err.message);
116 if (_.isNil(err.action)) {
117 err['action'] = messages.getMessage('invalidInstanceUrlForAccessTokenAction');
118 }
119 throw err;
120 }
121 return CheckStatus.OK;
122}
123exports.checkInvalidLoginUrlWithAccessToken = checkInvalidLoginUrlWithAccessToken;
124
125//# sourceMappingURL=indexErrorProcessor.js.map