fix(aio): ensure DocumentService doesn't crash on bad JSON

This commit is contained in:
Peter Bacon Darwin 2017-03-27 18:09:56 +01:00 committed by Pete Bacon Darwin
parent d28243d5fc
commit ff82756415
2 changed files with 18 additions and 1 deletions

View File

@ -105,6 +105,22 @@ describe('DocumentService', () => {
expect(currentDocument).toEqual(nextDoc);
});
it('should not crash the app if the response is not valid JSON', () => {
let latestDocument: DocumentContents;
const { service, backend, location } = getServices('initial/url');
const connections = backend.connectionsArray;
service.currentDocument.subscribe(doc => latestDocument = doc);
connections[0].mockRespond(new Response(new ResponseOptions({ body: 'this is invalid JSON' })));
expect(latestDocument.title).toEqual('Error fetching document');
const doc1 = { title: 'doc 1' };
location.urlSubject.next('new/url');
connections[1].mockRespond(createResponse(doc1));
expect(latestDocument).toEqual(doc1);
});
it('should not make a request to the server if the doc is in the cache already', () => {
let latestDocument: DocumentContents;
let subscription: Subscription;

View File

@ -54,7 +54,8 @@ export class DocumentService {
return Observable.of({ title: 'Not Found', contents: 'Document not found' });
}
} else {
throw error;
this.logger.error('Error fetching document', error);
return Observable.of({ title: 'Error fetching document', contents: 'Sorry we were not able to fetch that document.' });
}
})
.subscribe(subject);