diff --git a/aio/src/app/documents/document.service.spec.ts b/aio/src/app/documents/document.service.spec.ts index c9c7c3a17a..f53648094a 100644 --- a/aio/src/app/documents/document.service.spec.ts +++ b/aio/src/app/documents/document.service.spec.ts @@ -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; diff --git a/aio/src/app/documents/document.service.ts b/aio/src/app/documents/document.service.ts index de9b5da142..3b1968dd72 100644 --- a/aio/src/app/documents/document.service.ts +++ b/aio/src/app/documents/document.service.ts @@ -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);