Skip to content

Commit 5299677

Browse files
crisbetojelbourn
authored andcommitted
refactor: remove custom error classes (#4324)
* 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 6842046 commit 5299677

32 files changed

+170
-315
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
@@ -8,24 +8,20 @@ import {
88
InjectionToken,
99
} from '@angular/core';
1010
import {DOCUMENT} from '@angular/platform-browser';
11-
import {MdError} from '../errors/error';
1211

1312
export const MATERIAL_COMPATIBILITY_MODE = new InjectionToken<boolean>('md-compatibility-mode');
1413

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

1817
/**
19-
* Exception thrown if the consumer has used an invalid Material prefix on a component.
18+
* Returns an exception to be thrown if the consumer has used
19+
* an invalid Material prefix on a component.
2020
* @docs-private
2121
*/
22-
export class MdCompatibilityInvalidPrefixError extends MdError {
23-
constructor(prefix: string, nodeName: string) {
24-
super(
25-
`The "${prefix}-" prefix cannot be used in ng-material v1 compatibility mode. ` +
26-
`It was used on an "${nodeName.toLowerCase()}" element.`
27-
);
28-
}
22+
export function getMdCompatibilityInvalidPrefixError(prefix: string, nodeName: string) {
23+
return new Error(`The "${prefix}-" prefix cannot be used in ng-material v1 compatibility mode. ` +
24+
`It was used on an "${nodeName.toLowerCase()}" element.`);
2925
}
3026

3127
/** Selector that matches all elements that may have style collisions with AngularJS Material. */
@@ -160,7 +156,7 @@ export class MatPrefixRejector {
160156
elementRef: ElementRef) {
161157

162158
if (!isCompatibilityMode) {
163-
throw new MdCompatibilityInvalidPrefixError('mat', elementRef.nativeElement.nodeName);
159+
throw getMdCompatibilityInvalidPrefixError('mat', elementRef.nativeElement.nodeName);
164160
}
165161
}
166162
}
@@ -173,7 +169,7 @@ export class MdPrefixRejector {
173169
elementRef: ElementRef) {
174170

175171
if (isCompatibilityMode) {
176-
throw new MdCompatibilityInvalidPrefixError('md', elementRef.nativeElement.nodeName);
172+
throw getMdCompatibilityInvalidPrefixError('md', elementRef.nativeElement.nodeName);
177173
}
178174
}
179175
}

src/lib/core/core.ts

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

79-
// Error
80-
export {MdError} from './errors/error';
81-
8279
// Misc
8380
export {ComponentType} from './overlay/generic-component-type';
8481

src/lib/core/errors/error.ts

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

src/lib/core/portal/portal-errors.ts

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/datepicker/datepicker.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {OverlayRef} from '../core/overlay/overlay-ref';
1717
import {ComponentPortal} from '../core/portal/portal';
1818
import {OverlayState} from '../core/overlay/overlay-state';
1919
import {Dir} from '../core/rtl/dir';
20-
import {MdError} from '../core/errors/error';
2120
import {MdDialog} from '../dialog/dialog';
2221
import {MdDialogRef} from '../dialog/dialog-ref';
2322
import {PositionStrategy} from '../core/overlay/position/position-strategy';
@@ -192,7 +191,7 @@ export class MdDatepicker<D> implements OnDestroy {
192191
*/
193192
_registerInput(input: MdDatepickerInput<D>): void {
194193
if (this._datepickerInput) {
195-
throw new MdError('An MdDatepicker can only be associated with a single input.');
194+
throw new Error('An MdDatepicker can only be associated with a single input.');
196195
}
197196
this._datepickerInput = input;
198197
this._inputSubscription =
@@ -205,7 +204,7 @@ export class MdDatepicker<D> implements OnDestroy {
205204
return;
206205
}
207206
if (!this._datepickerInput) {
208-
throw new MdError('Attempted to open an MdDatepicker with no associated input.');
207+
throw new Error('Attempted to open an MdDatepicker with no associated input.');
209208
}
210209

211210
this.touchUi ? this._openAsDialog() : this._openAsPopup();

src/lib/dialog/dialog-container.ts

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

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

2835
/**
2936
* Internal component that wraps user-provided dialog content.
@@ -89,7 +96,7 @@ export class MdDialogContainer extends BasePortalHost {
8996
*/
9097
attachComponentPortal<T>(portal: ComponentPortal<T>): ComponentRef<T> {
9198
if (this._portalHost.hasAttached()) {
92-
throw new MdDialogContentAlreadyAttachedError();
99+
throwMdDialogContentAlreadyAttachedError();
93100
}
94101

95102
this._savePreviouslyFocusedElement();
@@ -102,7 +109,7 @@ export class MdDialogContainer extends BasePortalHost {
102109
*/
103110
attachTemplatePortal(portal: TemplatePortal): Map<string, any> {
104111
if (this._portalHost.hasAttached()) {
105-
throw new MdDialogContentAlreadyAttachedError();
112+
throwMdDialogContentAlreadyAttachedError();
106113
}
107114

108115
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)