Skip to content

Commit

Permalink
fix(router): improve error for missing base href
Browse files Browse the repository at this point in the history
Closes #3096
  • Loading branch information
btford committed Jul 17, 2015
1 parent 8296dce commit 011fab3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 9 additions & 2 deletions modules/angular2/src/router/location.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {LocationStrategy} from './location_strategy';
import {StringWrapper, isPresent, CONST_EXPR} from 'angular2/src/facade/lang';
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
import {BaseException, isBlank} from 'angular2/src/facade/lang';
import {OpaqueToken, Injectable, Optional, Inject} from 'angular2/di';

export const appBaseHrefToken: OpaqueToken = CONST_EXPR(new OpaqueToken('locationHrefToken'));
Expand All @@ -22,8 +23,14 @@ export class Location {

constructor(public _platformStrategy: LocationStrategy,
@Optional() @Inject(appBaseHrefToken) href?: string) {
this._baseHref = stripTrailingSlash(
stripIndexHtml(isPresent(href) ? href : this._platformStrategy.getBaseHref()));
var browserBaseHref = isPresent(href) ? href : this._platformStrategy.getBaseHref();

if (isBlank(browserBaseHref)) {
throw new BaseException(
`No base href set. Either provide a binding to "appBaseHrefToken" or add a base element.`);
}

this._baseHref = stripTrailingSlash(stripIndexHtml(browserBaseHref));
this._platformStrategy.onPopState((_) => this._onPopState(_));
}

Expand Down
8 changes: 8 additions & 0 deletions modules/angular2/test/router/location_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,13 @@ export function main() {
location.go('user/btford');
expect(locationStrategy.path()).toEqual('/my/custom/href/user/btford');
});

it('should throw when no base href is provided', () => {
var locationStrategy = new MockLocationStrategy();
locationStrategy.internalBaseHref = null;
expect(() => new Location(locationStrategy))
.toThrowError(
`No base href set. Either provide a binding to "appBaseHrefToken" or add a base element.`);
});
});
}

0 comments on commit 011fab3

Please sign in to comment.