private static void throwIfFatalVisitingError(VisitorControlHandler handler, RestUri restUri) throws RestApiException { final VisitorControlHandler.Result result = handler.getResult(); if (result.getCode() == VisitorControlHandler.CompletionCode.TIMEOUT) { if (! handler.hasVisitedAnyBuckets()) { throw new RestApiException(Response.createErrorResponse(500, "Timed out", restUri, RestUri.apiErrorCodes.TIME_OUT)); } // else: some progress has been made, let client continue with new token. } else if (result.getCode() != VisitorControlHandler.CompletionCode.SUCCESS) { throw new RestApiException(Response.createErrorResponse(400, result.toString(), RestUri.apiErrorCodes.VISITOR_ERROR)); } }