Skip to content

Commit e9a890a

Browse files
devversionmmalerba
authored andcommitted
chore(schematics): use devkit SchematicException for schematic errors (#14355)
* Makes all error throwings in the `cdk/schematics` and `lib/schematics` consistent by using `SchematicsException`. This is recommended by the CLI team so that the CLI can show different error-codes in the future (still planned AFAIK; but we want this change for consistency anyway)
1 parent 3f2751f commit e9a890a

File tree

8 files changed

+33
-20
lines changed

8 files changed

+33
-20
lines changed

src/cdk/schematics/ng-update/upgrade-data.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {SchematicsException} from '@angular-devkit/schematics';
910
import {RuleWalker} from 'tslint';
1011
import {
1112
attributeSelectors,
@@ -81,7 +82,8 @@ export const cdkUpgradeData: RuleUpgradeData = {
8182
*/
8283
export function getChangesForTarget<T>(target: TargetVersion, data: VersionChanges<T>): T[] {
8384
if (!data) {
84-
throw new Error(`No data could be found for target version: ${TargetVersion[target]}`);
85+
throw new SchematicsException(
86+
`No data could be found for target version: ${TargetVersion[target]}`);
8587
}
8688

8789
if (!data[target]) {

src/cdk/schematics/ng-update/upgrade-rules/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Rule, SchematicContext, TaskId, Tree} from '@angular-devkit/schematics';
9+
import {
10+
Rule,
11+
SchematicContext,
12+
SchematicsException,
13+
TaskId,
14+
Tree
15+
} from '@angular-devkit/schematics';
1016
import {RunSchematicTask, TslintFixTask} from '@angular-devkit/schematics/tasks';
1117
import {sync as globSync} from 'glob';
1218
import {getProjectTsConfigPaths} from './project-tsconfig-paths';
@@ -22,8 +28,8 @@ export function createUpgradeRule(targetVersion: TargetVersion,
2228
const tslintFixTasks: TaskId[] = [];
2329

2430
if (!projectTsConfigPaths.length) {
25-
throw new Error('Could not find any tsconfig file. Please submit an issue on the Angular ' +
26-
'Material repository that includes the name of your TypeScript configuration.');
31+
throw new SchematicsException('Could not find any tsconfig file. Please submit an issue ' +
32+
'on the Angular Material repository that includes the path to your "tsconfig" file.');
2733
}
2834
// In some applications, developers will have global stylesheets which are not specified in any
2935
// Angular component. Therefore we glob up all CSS and SCSS files outside of node_modules and

src/cdk/schematics/utils/ast/ng-module-imports.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Tree} from '@angular-devkit/schematics';
9+
import {SchematicsException, Tree} from '@angular-devkit/schematics';
1010
import * as ts from 'typescript';
1111

1212
/**
13-
* Whether the Angular module in the given path imports the specifed module class name.
13+
* Whether the Angular module in the given path imports the specified module class name.
1414
*/
1515
export function hasNgModuleImport(tree: Tree, modulePath: string, className: string): boolean {
1616
const moduleFileContent = tree.read(modulePath);
1717

1818
if (!moduleFileContent) {
19-
throw new Error(`Could not read Angular module file: ${modulePath}`);
19+
throw new SchematicsException(`Could not read Angular module file: ${modulePath}`);
2020
}
2121

2222
const parsedFile = ts.createSourceFile(modulePath, moduleFileContent.toString(),
@@ -36,7 +36,7 @@ export function hasNgModuleImport(tree: Tree, modulePath: string, className: str
3636
ts.forEachChild(parsedFile, findModuleDecorator);
3737

3838
if (!ngModuleMetadata) {
39-
throw new Error(`Could not find NgModule declaration inside: "${modulePath}"`);
39+
throw new SchematicsException(`Could not find NgModule declaration inside: "${modulePath}"`);
4040
}
4141

4242
for (let property of ngModuleMetadata!.properties) {

src/cdk/schematics/utils/get-project.ts

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

99
import {WorkspaceSchema, WorkspaceProject} from '@angular-devkit/core/src/workspace';
10+
import {SchematicsException} from '@angular-devkit/schematics';
1011

1112
/**
1213
* Finds the specified project configuration in the workspace. Throws an error if the project
@@ -19,7 +20,7 @@ export function getProjectFromWorkspace(
1920
const project = workspace.projects[projectName || workspace.defaultProject!];
2021

2122
if (!project) {
22-
throw new Error(`Could not find project in workspace: ${projectName}`);
23+
throw new SchematicsException(`Could not find project in workspace: ${projectName}`);
2324
}
2425

2526
return project;

src/cdk/schematics/utils/parse5-element.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {SchematicsException} from '@angular-devkit/schematics';
910
import {DefaultTreeElement} from 'parse5';
1011

1112
/** Determines the indentation of child elements for the given Parse5 element. */
@@ -14,8 +15,8 @@ export function getChildElementIndentation(element: DefaultTreeElement) {
1415
.find(node => node['tagName']) as DefaultTreeElement | null;
1516

1617
if ((childElement && !childElement.sourceCodeLocation) || !element.sourceCodeLocation) {
17-
throw new Error('Cannot determine child element indentation because the specified Parse5 ' +
18-
'element does not have any source code location metadata.');
18+
throw new SchematicsException('Cannot determine child element indentation because the ' +
19+
'specified Parse5 element does not have any source code location metadata.');
1920
}
2021

2122
const startColumns = childElement ?

src/cdk/schematics/utils/project-targets.ts

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

99
import {WorkspaceProject} from '@angular-devkit/core/src/workspace';
10+
import {SchematicsException} from '@angular-devkit/schematics';
1011

1112
/** Resolves the architect options for the build target of the given project. */
1213
export function getProjectTargetOptions(project: WorkspaceProject, buildTarget: string) {
@@ -27,5 +28,6 @@ export function getProjectTargetOptions(project: WorkspaceProject, buildTarget:
2728
return project.architect[buildTarget].options;
2829
}
2930

30-
throw new Error(`Cannot determine project target configuration for: ${buildTarget}.`);
31+
throw new SchematicsException(
32+
`Cannot determine project target configuration for: ${buildTarget}.`);
3133
}

src/cdk/schematics/utils/version-agnostic-typescript.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* dependency that will be shipped with `@schematics/angular`.
1414
*/
1515
import typescript = require('typescript');
16+
import {SchematicsException} from '@angular-devkit/schematics';
1617

1718
/**
1819
* This is an agnostic re-export of TypeScript. Depending on the context, this module file will
@@ -30,8 +31,8 @@ try {
3031
try {
3132
ts = require('typescript');
3233
} catch {
33-
throw new Error('Error: Could not find a TypeScript version for the schematics. ' +
34-
'Please report an issue on the Angular Material repository.');
34+
throw new SchematicsException('Error: Could not find a TypeScript version for the ' +
35+
'schematics. Please report an issue on the Angular Material repository.');
3536
}
3637
}
3738

src/lib/schematics/ng-add/theming/theming.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {normalize} from '@angular-devkit/core';
1010
import {WorkspaceProject, WorkspaceSchema} from '@angular-devkit/core/src/workspace';
11-
import {Tree} from '@angular-devkit/schematics';
11+
import {SchematicsException, Tree} from '@angular-devkit/schematics';
1212
import {
1313
getProjectFromWorkspace,
1414
getProjectStyleFile,
@@ -62,8 +62,8 @@ function insertCustomTheme(project: WorkspaceProject, projectName: string, host:
6262

6363
if (!stylesPath) {
6464
if (!project.sourceRoot) {
65-
throw new Error(`Could not find source root for project: "${projectName}". Please make ` +
66-
`sure that the "sourceRoot" property is set in the workspace config.`);
65+
throw new SchematicsException(`Could not find source root for project: "${projectName}". ` +
66+
`Please make sure that the "sourceRoot" property is set in the workspace config.`);
6767
}
6868

6969
// Normalize the path through the devkit utilities because we want to avoid having
@@ -159,9 +159,9 @@ function validateDefaultTargetBuilder(project: WorkspaceProject, targetName: 'bu
159159
// builder has been changed, we warn because a theme is not mandatory for running tests
160160
// with Material. See: https://github.com/angular/material2/issues/14176
161161
if (!isDefaultBuilder && targetName === 'build') {
162-
throw new Error(`Your project is not using the default builders for "${targetName}". The ` +
163-
`Angular Material schematics cannot add a theme to the workspace configuration if the ` +
164-
`builder has been changed. Exiting..`);
162+
throw new SchematicsException(`Your project is not using the default builders for ` +
163+
`"${targetName}". The Angular Material schematics cannot add a theme to the workspace ` +
164+
`configuration if the builder has been changed.`);
165165
} else if (!isDefaultBuilder) {
166166
console.warn(`Your project is not using the default builders for "${targetName}". This ` +
167167
`means that we cannot add the configured theme to the "${targetName}" target.`);

0 commit comments

Comments
 (0)