Skip to content

Commit 707c8fe

Browse files
authored
fix: display hook initialization errors (#224)
* fix: display hook initialization errors @W-10822822@ * chore: remove plugin name from error table * chore: review changes * chore: apply review suggestions
1 parent 3f92a8a commit 707c8fe

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

messages/deploy.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,11 @@ Force the CLI to prompt for all deployment inputs.
3232

3333
Can't find any active scratch orgs, Dev Hubs, or other orgs.
3434
Either log into an org or create a scratch org, and then try again.
35+
36+
# error.initialization
37+
38+
One or more initialization steps failed.
39+
40+
# error.initialization.title
41+
42+
Initialization Failures. The following table describes each failure:

src/commands/deploy.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import { EOL } from 'os';
9-
import { Flags } from '@oclif/core';
9+
import { Flags, Hook } from '@oclif/core';
1010
import { Messages } from '@salesforce/core';
1111
import { writeJson, pathExists, writeFile, readFile } from 'fs-extra';
1212
import { Env, parseJsonMap } from '@salesforce/kit';
@@ -46,6 +46,8 @@ export default class Deploy extends SfCommand<void> {
4646

4747
const hookResults = await SfHook.run(this.config, 'sf:deploy', options);
4848

49+
this.checkForHookFailures(hookResults);
50+
4951
let deployers = hookResults.successes.flatMap((s) => s.result);
5052

5153
if (deployers.length === 0) {
@@ -162,4 +164,22 @@ export default class Deploy extends SfCommand<void> {
162164
}
163165
return final;
164166
}
167+
public checkForHookFailures(hookResults: Hook.Result<Deployer[]>): void {
168+
if (hookResults.failures?.length) {
169+
// display a table of the errors encountered; Plugin Name, Error Message
170+
const columns = {
171+
errorName: { header: 'Error Name' },
172+
errorMessage: { header: 'Error Message' },
173+
};
174+
175+
const failureData = hookResults.failures.map((failure) => {
176+
return { errorName: failure.error.name, errorMessage: failure.error.message };
177+
});
178+
this.styledHeader(messages.getMessage('error.initialization.title'));
179+
this.table(failureData, columns);
180+
const err = messages.createError('error.initialization');
181+
err.data = hookResults.failures;
182+
throw err;
183+
}
184+
}
165185
}

test/commands/deploy.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,37 @@
44
* Licensed under the BSD 3-Clause license.
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
7+
import { expect } from 'chai';
8+
import { Hook } from '@oclif/core';
9+
import { Deployer } from '@salesforce/sf-plugins-core';
10+
import Deploy from '../../lib/commands/deploy';
11+
import Result = Hook.Result;
12+
13+
class TestDeploy extends Deploy {
14+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
15+
// @ts-ignore
16+
public constructor() {
17+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
18+
// @ts-ignore
19+
super([]);
20+
}
21+
public async run() {}
22+
}
23+
24+
describe('checkForHookFailures', () => {
25+
it('should not throw when no hook failures', () => {
26+
const testDeploy = new TestDeploy();
27+
testDeploy.checkForHookFailures({} as Result<Deployer[]>);
28+
expect(testDeploy).to.be.ok;
29+
});
30+
it('should throw when hook failures are present', () => {
31+
const testDeploy = new TestDeploy();
32+
const shouldThrow = () =>
33+
testDeploy.checkForHookFailures({
34+
successes: [],
35+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
36+
failures: [{ plugin: { name: 'foo' }, error: { name: 'fooerror', message: 'bad stuff happened' } }],
37+
} as Result<Deployer[]>);
38+
expect(shouldThrow).to.throw(/One or more initialization steps failed./);
39+
});
40+
});

0 commit comments

Comments
 (0)