Skip to content

refactor: remove custom error classes #4324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/lib/core/compatibility/compatibility.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
NoConflictStyleCompatibilityMode,
MAT_ELEMENTS_SELECTOR,
MD_ELEMENTS_SELECTOR,
MdCompatibilityInvalidPrefixError,
getMdCompatibilityInvalidPrefixError,
} from './compatibility';
import {wrappedErrorMessage} from '../testing/wrapped-error-message';

Expand Down Expand Up @@ -34,7 +34,7 @@ describe('Style compatibility', () => {
}));

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

expect(() => {
TestBed.createComponent(ComponentWithMatCheckbox);
Expand All @@ -57,7 +57,7 @@ describe('Style compatibility', () => {
});

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

expect(() => {
TestBed.createComponent(ComponentWithMdCheckbox);
Expand All @@ -75,7 +75,7 @@ describe('Style compatibility', () => {
}));

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

expect(() => {
TestBed.createComponent(ComponentWithMdCheckbox);
Expand Down
18 changes: 7 additions & 11 deletions src/lib/core/compatibility/compatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,20 @@ import {
InjectionToken,
} from '@angular/core';
import {DOCUMENT} from '@angular/platform-browser';
import {MdError} from '../errors/error';

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

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

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

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

if (!isCompatibilityMode) {
throw new MdCompatibilityInvalidPrefixError('mat', elementRef.nativeElement.nodeName);
throw getMdCompatibilityInvalidPrefixError('mat', elementRef.nativeElement.nodeName);
}
}
}
Expand All @@ -173,7 +169,7 @@ export class MdPrefixRejector {
elementRef: ElementRef) {

if (isCompatibilityMode) {
throw new MdCompatibilityInvalidPrefixError('md', elementRef.nativeElement.nodeName);
throw getMdCompatibilityInvalidPrefixError('md', elementRef.nativeElement.nodeName);
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/lib/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ export {MdLineModule, MdLine, MdLineSetter} from './line/line';
// Style
export * from './style/index';

// Error
export {MdError} from './errors/error';

// Misc
export {ComponentType} from './overlay/generic-component-type';

Expand Down
12 changes: 0 additions & 12 deletions src/lib/core/errors/error.ts

This file was deleted.

55 changes: 20 additions & 35 deletions src/lib/core/portal/portal-errors.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,48 @@
import {MdError} from '../errors/error';

/**
* Exception thrown when attempting to attach a null portal to a host.
* Throws an exception when attempting to attach a null portal to a host.
* @docs-private
*/
export class NullPortalError extends MdError {
constructor() {
super('Must provide a portal to attach');
}
export function throwNullPortalError() {
throw new Error('Must provide a portal to attach');
}

/**
* Exception thrown when attempting to attach a portal to a host that is already attached.
* Throws an exception when attempting to attach a portal to a host that is already attached.
* @docs-private
*/
export class PortalAlreadyAttachedError extends MdError {
constructor() {
super('Host already has a portal attached');
}
export function throwPortalAlreadyAttachedError() {
throw new Error('Host already has a portal attached');
}

/**
* Exception thrown when attempting to attach a portal to an already-disposed host.
* Throws an exception when attempting to attach a portal to an already-disposed host.
* @docs-private
*/
export class PortalHostAlreadyDisposedError extends MdError {
constructor() {
super('This PortalHost has already been disposed');
}
export function throwPortalHostAlreadyDisposedError() {
throw new Error('This PortalHost has already been disposed');
}

/**
* Exception thrown when attempting to attach an unknown portal type.
* Throws an exception when attempting to attach an unknown portal type.
* @docs-private
*/
export class UnknownPortalTypeError extends MdError {
constructor() {
super(
'Attempting to attach an unknown Portal type. ' +
'BasePortalHost accepts either a ComponentPortal or a TemplatePortal.');
}
export function throwUnknownPortalTypeError() {
throw new Error('Attempting to attach an unknown Portal type. BasePortalHost accepts either' +
'a ComponentPortal or a TemplatePortal.');
}

/**
* Exception thrown when attempting to attach a portal to a null host.
* Throws an exception when attempting to attach a portal to a null host.
* @docs-private
*/
export class NullPortalHostError extends MdError {
constructor() {
super('Attempting to attach a portal to a null PortalHost');
}
export function throwNullPortalHostError() {
throw new Error('Attempting to attach a portal to a null PortalHost');
}

/**
* Exception thrown when attempting to detach a portal that is not attached.
* @docs-private
* Throws an exception when attempting to detach a portal that is not attached.
* @docs-privatew
*/
export class NoPortalAttachedError extends MdError {
constructor() {
super('Attempting to detach a portal that is not attached to a host');
}
export function throwNoPortalAttachedError() {
throw new Error('Attempting to detach a portal that is not attached to a host');
}
26 changes: 13 additions & 13 deletions src/lib/core/portal/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import {
Injector
} from '@angular/core';
import {
NullPortalHostError,
PortalAlreadyAttachedError,
NoPortalAttachedError,
NullPortalError,
PortalHostAlreadyDisposedError,
UnknownPortalTypeError
throwNullPortalHostError,
throwPortalAlreadyAttachedError,
throwNoPortalAttachedError,
throwNullPortalError,
throwPortalHostAlreadyDisposedError,
throwUnknownPortalTypeError
} from './portal-errors';
import {ComponentType} from '../overlay/generic-component-type';

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

if (host.hasAttached()) {
throw new PortalAlreadyAttachedError();
throwPortalAlreadyAttachedError();
}

this._attachedHost = host;
Expand All @@ -42,7 +42,7 @@ export abstract class Portal<T> {
detach(): void {
let host = this._attachedHost;
if (host == null) {
throw new NoPortalAttachedError();
throwNoPortalAttachedError();
}

this._attachedHost = null;
Expand Down Expand Up @@ -168,15 +168,15 @@ export abstract class BasePortalHost implements PortalHost {

attach(portal: Portal<any>): any {
if (!portal) {
throw new NullPortalError();
throwNullPortalError();
}

if (this.hasAttached()) {
throw new PortalAlreadyAttachedError();
throwPortalAlreadyAttachedError();
}

if (this._isDisposed) {
throw new PortalHostAlreadyDisposedError();
throwPortalHostAlreadyDisposedError();
}

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

throw new UnknownPortalTypeError();
throwUnknownPortalTypeError();
}

abstract attachComponentPortal<T>(portal: ComponentPortal<T>): ComponentRef<T>;
Expand Down
5 changes: 2 additions & 3 deletions src/lib/datepicker/datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {OverlayRef} from '../core/overlay/overlay-ref';
import {ComponentPortal} from '../core/portal/portal';
import {OverlayState} from '../core/overlay/overlay-state';
import {Dir} from '../core/rtl/dir';
import {MdError} from '../core/errors/error';
import {MdDialog} from '../dialog/dialog';
import {MdDialogRef} from '../dialog/dialog-ref';
import {PositionStrategy} from '../core/overlay/position/position-strategy';
Expand Down Expand Up @@ -192,7 +191,7 @@ export class MdDatepicker<D> implements OnDestroy {
*/
_registerInput(input: MdDatepickerInput<D>): void {
if (this._datepickerInput) {
throw new MdError('An MdDatepicker can only be associated with a single input.');
throw new Error('An MdDatepicker can only be associated with a single input.');
}
this._datepickerInput = input;
this._inputSubscription =
Expand All @@ -205,7 +204,7 @@ export class MdDatepicker<D> implements OnDestroy {
return;
}
if (!this._datepickerInput) {
throw new MdError('Attempted to open an MdDatepicker with no associated input.');
throw new Error('Attempted to open an MdDatepicker with no associated input.');
}

this.touchUi ? this._openAsDialog() : this._openAsPopup();
Expand Down
13 changes: 10 additions & 3 deletions src/lib/dialog/dialog-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ import {
import {DOCUMENT} from '@angular/platform-browser';
import {BasePortalHost, ComponentPortal, PortalHostDirective, TemplatePortal} from '../core';
import {MdDialogConfig} from './dialog-config';
import {MdDialogContentAlreadyAttachedError} from './dialog-errors';
import {FocusTrapFactory, FocusTrap} from '../core/a11y/focus-trap';

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

/**
* Internal component that wraps user-provided dialog content.
Expand Down Expand Up @@ -89,7 +96,7 @@ export class MdDialogContainer extends BasePortalHost {
*/
attachComponentPortal<T>(portal: ComponentPortal<T>): ComponentRef<T> {
if (this._portalHost.hasAttached()) {
throw new MdDialogContentAlreadyAttachedError();
throwMdDialogContentAlreadyAttachedError();
}

this._savePreviouslyFocusedElement();
Expand All @@ -102,7 +109,7 @@ export class MdDialogContainer extends BasePortalHost {
*/
attachTemplatePortal(portal: TemplatePortal): Map<string, any> {
if (this._portalHost.hasAttached()) {
throw new MdDialogContentAlreadyAttachedError();
throwMdDialogContentAlreadyAttachedError();
}

this._savePreviouslyFocusedElement();
Expand Down
11 changes: 0 additions & 11 deletions src/lib/dialog/dialog-errors.ts

This file was deleted.

31 changes: 0 additions & 31 deletions src/lib/grid-list/grid-list-errors.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/lib/grid-list/grid-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
import {MdGridTile} from './grid-tile';
import {TileCoordinator} from './tile-coordinator';
import {TileStyler, FitTileStyler, RatioTileStyler, FixedTileStyler} from './tile-styler';
import {MdGridListColsError} from './grid-list-errors';
import {Dir} from '../core';
import {
coerceToString,
Expand Down Expand Up @@ -97,7 +96,8 @@ export class MdGridList implements OnInit, AfterContentChecked {
/** Throw a friendly error if cols property is missing */
private _checkCols() {
if (!this.cols) {
throw new MdGridListColsError();
throw new Error(`md-grid-list: must pass in number of columns. ` +
`Example: <md-grid-list cols="3">`);
}
}

Expand Down
Loading