vendredi 11 décembre 2015

Design pattern for returning false instead of throwing error

I am working on a Python project to scrape data from various external sources. Everything is Object Oriented except for a few scripts that just instantiate some objects and call a couple methods. So all the logic is in different classes. I don't really have any concrete code for this. Basically I am trying I have a function that tries to scrape a URL and if successful returns the data else returns false.

The return result for this gets used by another function saveData() to save the data to a file. If the scrape was unsuccessful and returned false then saveData() returns false. The this gets passed on and lets say it keeps going.

The reason scrape() returns false instead of throwing an error is that I will use this in a script to scraper many URLs and I don't want it to die because of one bad URL.

I don't really like the idea of this "trickling down" effect, where I have to check for a false value all the way down, just to avoid an error.

The ideal solution would be to stop processing that URL once it fails to scrape it. I feel like this is a common situation that may have some design pattern to avoid it?

Maybe this is too abstract for SO.

Aucun commentaire:

Enregistrer un commentaire