Skip to content

Commit 71c2d68

Browse files
authored
feat(core): add an error event to monitor error from Algolia (#2642)
Fix #1585
1 parent 08a8747 commit 71c2d68

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

docgen/layouts/instantsearch.pug

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ block content
3636
a.anchor(href=`${navPath}#events`)
3737
p InstantSearch is an EventEmitter and as such it emits events on specific parts of the lifecycle.
3838
h3 render
39-
p
39+
p
4040
| The `render` event is triggered when the rendering of all the widgets is done. This
4141
| happens after a search result comes back from Algolia - which means that it is
4242
| triggered for the first once everything after all the widgets went through all
4343
| there lifecycle steps once (getConfiguration, init, render).
44+
h3 error
45+
p
46+
| The `error` event is triggered when an error is reported when calling the API.

src/lib/InstantSearch.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ Usage: instantsearch({
262262
this.helper = helper;
263263
this._init(helper.state, this.helper);
264264
this.helper.on('result', this._render.bind(this, this.helper));
265+
this.helper.on('error', e => {
266+
this.emit('error', e);
267+
});
265268

266269
this._searchStalledTimer = null;
267270
this._isSearchStalled = true;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// import algoliaSearchHelper from 'algoliasearch-helper';
2+
import InstantSearch from '../InstantSearch';
3+
4+
describe('InstantSearch lifecycle', () => {
5+
it('emits an error if the API returns an error', () => {
6+
const search = new InstantSearch({
7+
// correct credentials so that the client does not retry
8+
appId: 'latency',
9+
apiKey: '6be0576ff61c053d5f9a3225e2a90f76',
10+
// the index name does not exist so that we get an error
11+
indexName: 'DOESNOTEXIST',
12+
});
13+
14+
let sendError;
15+
let reject;
16+
const waitForError = new Promise((resolve, r) => {
17+
sendError = resolve;
18+
reject = r;
19+
});
20+
21+
search.on('error', e => {
22+
try {
23+
expect(e).toEqual(expect.any(Error));
24+
} catch (err) {
25+
reject(err);
26+
}
27+
sendError();
28+
});
29+
30+
search.addWidget({
31+
render: () => {},
32+
});
33+
34+
search.start();
35+
36+
return waitForError;
37+
});
38+
});

src/lib/__tests__/__snapshots__/InstantSearch-test.js.snap

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ Array [
77
"helper": AlgoliaSearchHelper {
88
"_currentNbQueries": 0,
99
"_events": Object {
10+
"error": [Function],
1011
"result": [Function],
1112
"search": [Function],
1213
},
13-
"_eventsCount": 2,
14+
"_eventsCount": 3,
1415
"_lastQueryIdReceived": -1,
1516
"_queryId": 0,
1617
"client": Object {
@@ -94,10 +95,11 @@ Array [
9495
"helper": AlgoliaSearchHelper {
9596
"_currentNbQueries": 0,
9697
"_events": Object {
98+
"error": [Function],
9799
"result": [Function],
98100
"search": [Function],
99101
},
100-
"_eventsCount": 2,
102+
"_eventsCount": 3,
101103
"_lastQueryIdReceived": -1,
102104
"_queryId": 0,
103105
"client": Object {

0 commit comments

Comments
 (0)