Skip to content

Commit f3ee19e

Browse files
committed
refactor: remove custom error classes
* Removes the custom `MdError` class and refactors all of the classes that extended it into functions. * Removes a few of the single-function `*-errors.ts` files and inlines their content. Fixes #4317.
1 parent f16affc commit f3ee19e

28 files changed

+146
-281
lines changed

src/lib/core/compatibility/compatibility.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
NoConflictStyleCompatibilityMode,
66
MAT_ELEMENTS_SELECTOR,
77
MD_ELEMENTS_SELECTOR,
8-
MdCompatibilityInvalidPrefixError,
8+
getMdCompatibilityInvalidPrefixError,
99
} from './compatibility';
1010
import {wrappedErrorMessage} from '../testing/wrapped-error-message';
1111

@@ -34,7 +34,7 @@ describe('Style compatibility', () => {
3434
}));
3535

3636
it('should throw an error when trying to use the "mat-" prefix', () => {
37-
const expectedError = new MdCompatibilityInvalidPrefixError('mat', 'mat-checkbox');
37+
const expectedError = getMdCompatibilityInvalidPrefixError('mat', 'mat-checkbox');
3838

3939
expect(() => {
4040
TestBed.createComponent(ComponentWithMatCheckbox);
@@ -57,7 +57,7 @@ describe('Style compatibility', () => {
5757
});
5858

5959
it('should throw an error when trying to use the "md-" prefix', () => {
60-
const expectedError = new MdCompatibilityInvalidPrefixError('md', 'md-checkbox');
60+
const expectedError = getMdCompatibilityInvalidPrefixError('md', 'md-checkbox');
6161

6262
expect(() => {
6363
TestBed.createComponent(ComponentWithMdCheckbox);
@@ -75,7 +75,7 @@ describe('Style compatibility', () => {
7575
}));
7676

7777
it('should throw an error when using the "md-" prefix', () => {
78-
const expectedError = new MdCompatibilityInvalidPrefixError('md', 'md-checkbox');
78+
const expectedError = getMdCompatibilityInvalidPrefixError('md', 'md-checkbox');
7979

8080
expect(() => {
8181
TestBed.createComponent(ComponentWithMdCheckbox);

src/lib/core/compatibility/compatibility.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,20 @@ import {
1010
InjectionToken,
1111
} from '@angular/core';
1212
import {DOCUMENT} from '@angular/platform-browser';
13-
import {MdError} from '../errors/error';
1413

1514
export const MATERIAL_COMPATIBILITY_MODE = new OpaqueToken('md-compatibility-mode');
1615

1716
/** Injection token that configures whether the Material sanity checks are enabled. */
1817
export const MATERIAL_SANITY_CHECKS = new InjectionToken<boolean>('md-sanity-checks');
1918

2019
/**
21-
* Exception thrown if the consumer has used an invalid Material prefix on a component.
20+
* Returns an exception to be thrown if the consumer has used
21+
* an invalid Material prefix on a component.
2222
* @docs-private
2323
*/
24-
export class MdCompatibilityInvalidPrefixError extends MdError {
25-
constructor(prefix: string, nodeName: string) {
26-
super(
27-
`The "${prefix}-" prefix cannot be used in ng-material v1 compatibility mode. ` +
28-
`It was used on an "${nodeName.toLowerCase()}" element.`
29-
);
30-
}
24+
export function getMdCompatibilityInvalidPrefixError(prefix: string, nodeName: string) {
25+
return new Error(`The "${prefix}-" prefix cannot be used in ng-material v1 compatibility mode. ` +
26+
`It was used on an "${nodeName.toLowerCase()}" element.`);
3127
}
3228

3329
/** Selector that matches all elements that may have style collisions with AngularJS Material. */
@@ -162,7 +158,7 @@ export class MatPrefixRejector {
162158
elementRef: ElementRef) {
163159

164160
if (!isCompatibilityMode) {
165-
throw new MdCompatibilityInvalidPrefixError('mat', elementRef.nativeElement.nodeName);
161+
throw getMdCompatibilityInvalidPrefixError('mat', elementRef.nativeElement.nodeName);
166162
}
167163
}
168164
}
@@ -175,7 +171,7 @@ export class MdPrefixRejector {
175171
elementRef: ElementRef) {
176172

177173
if (isCompatibilityMode) {
178-
throw new MdCompatibilityInvalidPrefixError('md', elementRef.nativeElement.nodeName);
174+
throw getMdCompatibilityInvalidPrefixError('md', elementRef.nativeElement.nodeName);
179175
}
180176
}
181177
}

src/lib/core/core.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ export {MdLineModule, MdLine, MdLineSetter} from './line/line';
8989
// Style
9090
export * from './style/index';
9191

92-
// Error
93-
export {MdError} from './errors/error';
94-
9592
// Misc
9693
export {ComponentType} from './overlay/generic-component-type';
9794

src/lib/core/errors/error.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,48 @@
1-
import {MdError} from '../errors/error';
2-
31
/**
4-
* Exception thrown when attempting to attach a null portal to a host.
2+
* Throws an exception when attempting to attach a null portal to a host.
53
* @docs-private
64
*/
7-
export class NullPortalError extends MdError {
8-
constructor() {
9-
super('Must provide a portal to attach');
10-
}
5+
export function throwNullPortalError() {
6+
throw new Error('Must provide a portal to attach');
117
}
128

139
/**
14-
* Exception thrown when attempting to attach a portal to a host that is already attached.
10+
* Throws an exception when attempting to attach a portal to a host that is already attached.
1511
* @docs-private
1612
*/
17-
export class PortalAlreadyAttachedError extends MdError {
18-
constructor() {
19-
super('Host already has a portal attached');
20-
}
13+
export function throwPortalAlreadyAttachedError() {
14+
throw new Error('Host already has a portal attached');
2115
}
2216

2317
/**
24-
* Exception thrown when attempting to attach a portal to an already-disposed host.
18+
* Throws an exception when attempting to attach a portal to an already-disposed host.
2519
* @docs-private
2620
*/
27-
export class PortalHostAlreadyDisposedError extends MdError {
28-
constructor() {
29-
super('This PortalHost has already been disposed');
30-
}
21+
export function throwPortalHostAlreadyDisposedError() {
22+
throw new Error('This PortalHost has already been disposed');
3123
}
3224

3325
/**
34-
* Exception thrown when attempting to attach an unknown portal type.
26+
* Throws an exception when attempting to attach an unknown portal type.
3527
* @docs-private
3628
*/
37-
export class UnknownPortalTypeError extends MdError {
38-
constructor() {
39-
super(
40-
'Attempting to attach an unknown Portal type. ' +
41-
'BasePortalHost accepts either a ComponentPortal or a TemplatePortal.');
42-
}
29+
export function throwUnknownPortalTypeError() {
30+
throw new Error('Attempting to attach an unknown Portal type. BasePortalHost accepts either' +
31+
'a ComponentPortal or a TemplatePortal.');
4332
}
4433

4534
/**
46-
* Exception thrown when attempting to attach a portal to a null host.
35+
* Throws an exception when attempting to attach a portal to a null host.
4736
* @docs-private
4837
*/
49-
export class NullPortalHostError extends MdError {
50-
constructor() {
51-
super('Attempting to attach a portal to a null PortalHost');
52-
}
38+
export function throwNullPortalHostError() {
39+
throw new Error('Attempting to attach a portal to a null PortalHost');
5340
}
5441

5542
/**
56-
* Exception thrown when attempting to detach a portal that is not attached.
57-
* @docs-private
43+
* Throws an exception when attempting to detach a portal that is not attached.
44+
* @docs-privatew
5845
*/
59-
export class NoPortalAttachedError extends MdError {
60-
constructor() {
61-
super('Attempting to detach a portal that is not attached to a host');
62-
}
46+
export function throwNoPortalAttachedError() {
47+
throw new Error('Attempting to detach a portal that is not attached to a host');
6348
}

src/lib/core/portal/portal.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import {
66
Injector
77
} from '@angular/core';
88
import {
9-
NullPortalHostError,
10-
PortalAlreadyAttachedError,
11-
NoPortalAttachedError,
12-
NullPortalError,
13-
PortalHostAlreadyDisposedError,
14-
UnknownPortalTypeError
9+
throwNullPortalHostError,
10+
throwPortalAlreadyAttachedError,
11+
throwNoPortalAttachedError,
12+
throwNullPortalError,
13+
throwPortalHostAlreadyDisposedError,
14+
throwUnknownPortalTypeError
1515
} from './portal-errors';
1616
import {ComponentType} from '../overlay/generic-component-type';
1717

@@ -27,11 +27,11 @@ export abstract class Portal<T> {
2727
/** Attach this portal to a host. */
2828
attach(host: PortalHost): T {
2929
if (host == null) {
30-
throw new NullPortalHostError();
30+
throwNullPortalHostError();
3131
}
3232

3333
if (host.hasAttached()) {
34-
throw new PortalAlreadyAttachedError();
34+
throwPortalAlreadyAttachedError();
3535
}
3636

3737
this._attachedHost = host;
@@ -42,7 +42,7 @@ export abstract class Portal<T> {
4242
detach(): void {
4343
let host = this._attachedHost;
4444
if (host == null) {
45-
throw new NoPortalAttachedError();
45+
throwNoPortalAttachedError();
4646
}
4747

4848
this._attachedHost = null;
@@ -168,15 +168,15 @@ export abstract class BasePortalHost implements PortalHost {
168168

169169
attach(portal: Portal<any>): any {
170170
if (!portal) {
171-
throw new NullPortalError();
171+
throwNullPortalError();
172172
}
173173

174174
if (this.hasAttached()) {
175-
throw new PortalAlreadyAttachedError();
175+
throwPortalAlreadyAttachedError();
176176
}
177177

178178
if (this._isDisposed) {
179-
throw new PortalHostAlreadyDisposedError();
179+
throwPortalHostAlreadyDisposedError();
180180
}
181181

182182
if (portal instanceof ComponentPortal) {
@@ -187,7 +187,7 @@ export abstract class BasePortalHost implements PortalHost {
187187
return this.attachTemplatePortal(portal);
188188
}
189189

190-
throw new UnknownPortalTypeError();
190+
throwUnknownPortalTypeError();
191191
}
192192

193193
abstract attachComponentPortal<T>(portal: ComponentPortal<T>): ComponentRef<T>;

src/lib/dialog/dialog-container.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@ import {
2020
import {DOCUMENT} from '@angular/platform-browser';
2121
import {BasePortalHost, ComponentPortal, PortalHostDirective, TemplatePortal} from '../core';
2222
import {MdDialogConfig} from './dialog-config';
23-
import {MdDialogContentAlreadyAttachedError} from './dialog-errors';
2423
import {FocusTrapFactory, FocusTrap} from '../core/a11y/focus-trap';
2524

25+
/**
26+
* Throws an exception for the case when a ComponentPortal is
27+
* attached to a DomPortalHost without an origin.
28+
* @docs-private
29+
*/
30+
export function throwMdDialogContentAlreadyAttachedError() {
31+
throw new Error('Attempting to attach dialog content after content is already attached');
32+
}
2633

2734
/**
2835
* Internal component that wraps user-provided dialog content.
@@ -88,7 +95,7 @@ export class MdDialogContainer extends BasePortalHost {
8895
*/
8996
attachComponentPortal<T>(portal: ComponentPortal<T>): ComponentRef<T> {
9097
if (this._portalHost.hasAttached()) {
91-
throw new MdDialogContentAlreadyAttachedError();
98+
throwMdDialogContentAlreadyAttachedError();
9299
}
93100

94101
this._savePreviouslyFocusedElement();
@@ -101,7 +108,7 @@ export class MdDialogContainer extends BasePortalHost {
101108
*/
102109
attachTemplatePortal(portal: TemplatePortal): Map<string, any> {
103110
if (this._portalHost.hasAttached()) {
104-
throw new MdDialogContentAlreadyAttachedError();
111+
throwMdDialogContentAlreadyAttachedError();
105112
}
106113

107114
this._savePreviouslyFocusedElement();

src/lib/dialog/dialog-errors.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/lib/grid-list/grid-list-errors.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/lib/grid-list/grid-list.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
import {MdGridTile} from './grid-tile';
1414
import {TileCoordinator} from './tile-coordinator';
1515
import {TileStyler, FitTileStyler, RatioTileStyler, FixedTileStyler} from './tile-styler';
16-
import {MdGridListColsError} from './grid-list-errors';
1716
import {Dir} from '../core';
1817
import {
1918
coerceToString,
@@ -97,7 +96,8 @@ export class MdGridList implements OnInit, AfterContentChecked {
9796
/** Throw a friendly error if cols property is missing */
9897
private _checkCols() {
9998
if (!this.cols) {
100-
throw new MdGridListColsError();
99+
throw new Error(`md-grid-list: must pass in number of columns. ` +
100+
`Example: <md-grid-list cols="3">`);
101101
}
102102
}
103103

0 commit comments

Comments
 (0)