-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathcall_response_handlers.js
57 lines (45 loc) · 1.4 KB
/
call_response_handlers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { RequestFailure, SearchTimeout, ShardFailure } from 'ui/errors';
import { ReqStatusProvider } from './req_status';
import { CourierNotifierProvider } from './notifier';
export function CallResponseHandlersProvider(Private, Promise) {
const ABORTED = Private(ReqStatusProvider).ABORTED;
const INCOMPLETE = Private(ReqStatusProvider).INCOMPLETE;
const notify = Private(CourierNotifierProvider);
function callResponseHandlers(requests, responses) {
return Promise.map(requests, function (req, i) {
if (req === ABORTED || req.aborted) {
return ABORTED;
}
let resp = responses[i];
if (resp.timed_out) {
notify.warning(new SearchTimeout());
}
if (resp._shards && resp._shards.failed) {
notify.warning(new ShardFailure(resp));
}
function progress() {
if (req.isIncomplete()) {
return INCOMPLETE;
}
req.complete();
return resp;
}
if (resp.error) {
if (req.filterError(resp)) {
return progress();
} else {
return req.handleFailure(new RequestFailure(null, resp));
}
}
return Promise.try(function () {
return req.transformResponse(resp);
})
.then(function () {
resp = arguments[0];
return req.handleResponse(resp);
})
.then(progress);
});
}
return callResponseHandlers;
}