Hi @Jekabsons, thanks for your question. Agreed – the behavior is not immediately obvious, and, perhaps, somewhat unexpected.
By their nature, batch operations may complete/fail partially (IIRC that’s the only place you’d find such error handling), in which case you might want to retry inserting failed objects.
In contrast, the err
returned from Do(ctx)
means the operation has failed entirely (due to a network issue, malformed request, etc).
That being said, I totally see your point – to properly handle possible batch insert errors one’d need to iterate over response objects every time, even if the entire batch completed successfully. What would make more sense is having a custom PartialInsertError
type (or sth to that extent) with a slice of objects that failed, so you could handle the errors like so:
r, err := batch.Do(ctx)
if err != nil {
if partial, ok := err.(PartialInsertError); ok {
for _, obj := range err.Failed {
// insert again
}
}
}
// happy path -- all objects inserted successfully
What do you think?
I didn’t really see anywhere in documentation addressing this issue of how to properly handle such errors
Yes, I just double-checked our Batch Import docs and seems that this section is missing for all clients but the latest Python v4
. We’ll add an example of correct error handling to make sure this doesn’t catch others off-guard.