Skip to content

Commit 04f423b

Browse files
committed
refactor: cleanup error handling
1 parent fcc4090 commit 04f423b

File tree

5 files changed

+38
-43
lines changed

5 files changed

+38
-43
lines changed

src/angular-cli-ghpages

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ var path = require('path'),
66
pjson = require('./package.json'),
77
commander = require('commander');
88

9-
// unfortunately defaults-file is ignored for --no-silent & --no-dotfiles
10-
// according to PR it should work (https://github.com/tj/commander.js/issues/928) but it's still completely foobar!?!
9+
// defaults-file was ignored for --no-silent & --no-dotfiles, so it is not used here
10+
// TODO: review https://github.com/tj/commander.js/issues/928 and check again if it now works
11+
// (this is not a big problem, because true is currently the desired value and this is also the value in the defaults file)
1112
commander
1213
.version(pjson.version)
1314
.description(pjson.description)
@@ -74,5 +75,7 @@ var consoleLogger = {
7475
var dir = path.join(process.cwd(), commander.dir);
7576

7677
engine.run(dir, commander, consoleLogger).catch(function(error) {
78+
consoleLogger.error('❌ An error occurred when trying to deploy:');
79+
consoleLogger.error(error.message);
7780
process.exit(1);
7881
});

src/deploy/actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default async function deploy(
4848
const buildResult = await build.result;
4949

5050
if (!buildResult.success) {
51-
throw new Error(buildResult.error);
51+
throw new Error("Error while building the app.");
5252
}
5353
}
5454

src/deploy/builder.ts

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,46 +16,47 @@ import { Schema } from './schema';
1616
// createJobHandler() but add typings specific to Architect Builders.
1717
export default createBuilder<any>(
1818
async (options: Schema, context: BuilderContext): Promise<BuilderOutput> => {
19-
// The project root is added to a BuilderContext.
20-
const root = normalize(context.workspaceRoot);
21-
const workspace = new experimental.workspace.Workspace(
22-
root,
23-
new NodeJsSyncHost()
24-
);
25-
await workspace
26-
.loadWorkspaceFromHost(normalize('angular.json'))
27-
.toPromise();
19+
try {
20+
// The project root is added to a BuilderContext.
21+
const root = normalize(context.workspaceRoot);
22+
const workspace = new experimental.workspace.Workspace(
23+
root,
24+
new NodeJsSyncHost()
25+
);
26+
await workspace
27+
.loadWorkspaceFromHost(normalize('angular.json'))
28+
.toPromise();
2829

29-
if (!context.target) {
30-
throw new Error('Cannot deploy the application without a target');
31-
}
30+
if (!context.target) {
31+
throw new Error('Cannot deploy the application without a target');
32+
}
3233

33-
const targets = workspace.getProjectTargets(context.target.project);
34+
const targets = workspace.getProjectTargets(context.target.project);
3435

35-
if (
36-
!targets ||
37-
!targets.build ||
38-
!targets.build.options ||
39-
!targets.build.options.outputPath
40-
) {
41-
throw new Error('Cannot find the project output directory');
42-
}
36+
if (
37+
!targets ||
38+
!targets.build ||
39+
!targets.build.options ||
40+
!targets.build.options.outputPath
41+
) {
42+
throw new Error('Cannot find the project output directory');
43+
}
4344

44-
const isWin = os.platform() === 'win32';
45-
const workspaceRoot = !isWin
46-
? workspace.root
47-
: asWindowsPath(workspace.root);
45+
const isWin = os.platform() === 'win32';
46+
const workspaceRoot = !isWin
47+
? workspace.root
48+
: asWindowsPath(workspace.root);
4849

49-
try {
5050
await deploy(
5151
engine,
5252
context,
5353
path.join(workspaceRoot, targets.build.options.outputPath),
5454
options
5555
);
5656
} catch (e) {
57-
context.logger.error('Error when trying to deploy:', e);
58-
console.error(e);
57+
58+
context.logger.error('❌ An error occurred when trying to deploy:');
59+
context.logger.error(e.message);
5960
return { success: false };
6061
}
6162

src/engine/engine.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export async function run(
2323
ghpages.clean();
2424
}
2525

26-
try {
2726
await checkIfDistFolderExists(dir);
2827
await createNotFoundPage(dir, options, logger);
2928
await createCnameFile(dir, options, logger);
@@ -32,10 +31,6 @@ export async function run(
3231
logger.info(
3332
'🚀 Successfully published via angular-cli-ghpages! Have a nice day!'
3433
);
35-
} catch (error) {
36-
logger.error('❌ An error occurred!');
37-
throw error;
38-
}
3934
}
4035

4136
export function prepareOptions(origOptions: Schema, logger: logging.LoggerApi) {
@@ -138,9 +133,6 @@ async function createNotFoundPage(
138133
const indexHtml = path.join(dir, 'index.html');
139134
const notFoundPage = path.join(dir, '404.html');
140135

141-
// console.log('***', indexHtml)
142-
// console.log('***', notFoundPage)
143-
144136
try {
145137
return await fse.copy(indexHtml, notFoundPage);
146138
} catch (err) {
@@ -150,7 +142,7 @@ async function createNotFoundPage(
150142
logger.info(
151143
'(Hint: are you sure that you have setup the directory correctly?)'
152144
);
153-
logger.debug('Diagnostic info', err);
145+
logger.debug('Diagnostic info: ' + err.message);
154146
return;
155147
}
156148
}
@@ -176,8 +168,7 @@ async function createCnameFile(
176168
await fse.writeFile(cnameFile, options.cname);
177169
logger.info('CNAME file created');
178170
} catch (err) {
179-
logger.error('CNAME file could not be created. Stopping execution.');
180-
throw err;
171+
throw new Error('CNAME file could not be created. ' + err.message);
181172
}
182173
}
183174

src/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export interface GHPages {
2-
publish(dir: string, options: any): Promise<any>;
2+
publish(dir: string, options: any, callback: (error: any) => void);
33
clean?(): void;
44
}

0 commit comments

Comments
 (0)