Replace this cursor->next() with a cursor->nextSafe() call.
According to this comment, a nextSafe() should be used if you may get remote errors.
At first sight, this may not seem necessary in this case since the query is executed against the DbDirectClient (which means it performs a local query).
However it feels safer to call nextSafe() in any case, just to make sure we handle all the errors properly.