error handling for distance matrix