Commit f936fffa authored by Lyza Danger Gardner's avatar Lyza Danger Gardner Committed by Lyza Gardner

Update loadAnnotationsService to take optional `onError` option

parent 96425a93
......@@ -19,9 +19,11 @@
* with the expected presentation order of annotations/threads in the current
* view.
* @prop {SortOrder} [sortOrder]
* @prop {(error: Error) => any} [onError] - Optional error handler for
* SearchClient. Default error handling logs errors to console.
*/
import SearchClient from '../search-client';
import { SearchClient } from '../search-client';
import { isReply } from '../helpers/annotation-metadata';
......@@ -43,7 +45,7 @@ export default function loadAnnotationsService(
* @param {LoadAnnotationOptions} options
*/
function load(options) {
const { groupId, uris } = options;
const { groupId, onError, uris } = options;
store.removeAnnotations(store.savedAnnotations());
// Cancel previously running search client.
......@@ -93,7 +95,11 @@ export default function loadAnnotationsService(
});
searchClient.on('error', error => {
console.error(error);
if (typeof onError === 'function') {
onError(error);
} else {
console.error(error);
}
});
searchClient.on('end', () => {
......
......@@ -85,7 +85,9 @@ describe('loadAnnotationsService', () => {
fakeUris = ['http://example.com'];
$imports.$mock({
'../search-client': FakeSearchClient,
'../search-client': {
SearchClient: FakeSearchClient,
},
});
});
......@@ -304,7 +306,7 @@ describe('loadAnnotationsService', () => {
assert.calledOnce(fakeStore.annotationFetchFinished);
});
it('logs an error to the console if the search client runs into an error', () => {
it('logs an error by default to the console if the search client emits an error', () => {
const svc = createService();
const error = new Error('search for annotations failed');
......@@ -313,6 +315,17 @@ describe('loadAnnotationsService', () => {
assert.calledWith(console.error, error);
});
it('invokes error callback, if provided, when search client emits an error', () => {
const svc = createService();
const onError = sinon.stub();
const error = new Error('Something went wrong');
svc.load({ groupId: fakeGroupId, uris: fakeUris, onError });
searchClients[0].emit('error', error);
assert.calledWith(onError, error);
});
});
describe('loadThread', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment