fix(aio): emit a hard-coded doc if file-not-found fails

Closes #14848
This commit is contained in:
Peter Bacon Darwin 2017-03-04 17:00:54 +00:00 committed by Igor Minar
parent 4abd6f333c
commit d6c1ccaf14
2 changed files with 34 additions and 5 deletions

View File

@ -78,7 +78,31 @@ describe('DocumentService', () => {
});
it('should emit the not-found document if the document is not found on the server', () => {
const { service, backend } = getServices('missing/url');
const connections = backend.connectionsArray;
service.currentDocument.subscribe();
connections[0].mockError(new Response(new ResponseOptions({ status: 404, statusText: 'NOT FOUND'})) as any);
expect(connections.length).toEqual(2);
expect(connections[1].request.url).toEqual(CONTENT_URL_PREFIX + 'file-not-found.json');
});
it('should emit a hard-coded not-found document if the not-found document is not found on the server', () => {
let currentDocument: DocumentContents;
const notFoundDoc = { title: 'Not Found', contents: 'Document not found' };
const nextDoc = { title: 'Next Doc' };
const { service, backend, location } = getServices('file-not-found');
const connections = backend.connectionsArray;
service.currentDocument.subscribe(doc => currentDocument = doc);
connections[0].mockError(new Response(new ResponseOptions({ status: 404, statusText: 'NOT FOUND'})) as any);
expect(connections.length).toEqual(1);
expect(currentDocument).toEqual(notFoundDoc);
// now check that we haven't killed the currentDocument observable sequence
location.urlSubject.next('new/url');
connections[1].mockRespond(createResponse(nextDoc));
expect(currentDocument).toEqual(nextDoc);
});
it('should not make a request to the server if the doc is in the cache already', () => {

View File

@ -8,7 +8,7 @@ import 'rxjs/add/operator/switchMap';
import { LocationService } from 'app/shared/location.service';
import { Logger } from 'app/shared/logger.service';
const FILE_NOT_FOUND_DOC = 'file-not-found';
const FILE_NOT_FOUND_URL = 'file-not-found';
export interface DocumentContents {
title: string;
@ -19,6 +19,7 @@ export interface DocumentContents {
export class DocumentService {
private cache = new Map<string, Observable<DocumentContents>>();
private fileNotFoundPath = this.computePath(FILE_NOT_FOUND_URL);
currentDocument: Observable<DocumentContents>;
@ -43,10 +44,14 @@ export class DocumentService {
.get(path)
.map(res => res.json())
.catch((error: Response) => {
if (error.status === 404 && path !== FILE_NOT_FOUND_DOC) {
this.logger.error(`Document file not found at '${path}'`);
// using `getDocument` means that we can fetch the 404 doc contents from the server and cache it
return this.getDocument(FILE_NOT_FOUND_DOC);
if (error.status === 404) {
if (path !== this.fileNotFoundPath) {
this.logger.error(`Document file not found at '${path}'`);
// using `getDocument` means that we can fetch the 404 doc contents from the server and cache it
return this.getDocument(FILE_NOT_FOUND_URL);
} else {
return Observable.of({ title: 'Not Found', contents: 'Document not found' });
}
} else {
throw error;
}