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
|
# File 'lib/debugbundle/acknowledgement.rb', line 14
def decide(body, batch_length)
return { kind: :legacy } unless body.is_a?(Hash) && FIELDS.any? { |field| body.key?(field) }
return protocol_failure unless FIELDS.all? { |field| body.key?(field) }
accepted = body['accepted']
rejected = body['rejected']
errors = body['errors']
return protocol_failure unless count?(accepted) && count?(rejected) && errors.is_a?(Array)
return protocol_failure unless accepted + rejected == batch_length && errors.length == rejected
seen = {}
retryable_indices = []
terminal_errors = []
errors.each do |error|
return protocol_failure unless error.is_a?(Hash)
index = error['index']
reason = error['reason']
return protocol_failure unless index.is_a?(Integer) && index.between?(0, batch_length - 1)
return protocol_failure unless reason.is_a?(String) && !reason.empty? && !seen[index]
seen[index] = true
if RETRYABLE_REASONS.include?(reason)
retryable_indices << index
else
terminal_errors << [index, reason]
end
end
{
kind: :acknowledged,
accepted: accepted,
retryable_indices: retryable_indices,
terminal_errors: terminal_errors
}
end
|