@@ -94,12 +94,14 @@ def main():
9494 logger .error ("Ouput '%s' doesn't exist." , args .output )
9595 sys .exit (1 )
9696
97- cache = None
9897 retcode = 0
99- try :
100- cache = read_cache (logger , args .cache_file )
101- source = cache .get_source_by_url (args .rss_url )
98+ cache = wrap_read_cache (logger , args .cache_file )
99+ if cache is None :
100+ retcode = utils .mask_retcode (1 , args .mask_errors )
101+ sys .exit (retcode )
102102
103+ source = cache .get_source_by_url (args .rss_url )
104+ try :
103105 rsp = get_rss (
104106 logger ,
105107 args .rss_url ,
@@ -122,15 +124,6 @@ def main():
122124 cache .scrub_data_sources ()
123125 source .http_error_count = 0
124126 retcode = 0
125- except CacheReadError :
126- logger .exception (
127- "Error while reading cache file '%s'." ,
128- args .cache_file ,
129- )
130- retcode = utils .mask_retcode (1 , args .mask_errors )
131- # NOTE(zstyblik): since cache file couldn't be opened, it doesn't make
132- # sense writing it. Therefore, call sys.exit().
133- sys .exit (retcode )
134127 except NotModifiedError :
135128 logger .debug ("No new RSS data since the last run." )
136129 update_items_expiration (cache , cache .items , args .cache_expiration )
@@ -149,10 +142,7 @@ def main():
149142 retcode = 0
150143 except Exception :
151144 logger .exception ("Unexpected exception has occurred." )
152- if cache :
153- source = cache .get_source_by_url (args .rss_url )
154- source .http_error_count += 1
155-
145+ source .http_error_count += 1
156146 retcode = 1
157147
158148 write_retcode = wrap_write_cache (logger , cache , args .cache_file )
@@ -271,6 +261,18 @@ def update_items_expiration(
271261 cache .items [key ] = item_expiration
272262
273263
264+ def wrap_read_cache (logger : logging .Logger , cache_file : str ):
265+ """Call read_cache() and return cached data or log error and return None."""
266+ cache = None
267+ try :
268+ cache = read_cache (logger , cache_file )
269+ except CacheReadError :
270+ logger .exception ("Error while reading cache file '%s'." , cache_file )
271+ cache = None
272+
273+ return cache
274+
275+
274276def wrap_write_cache (
275277 logger : logging .Logger ,
276278 cache : CachedData ,
0 commit comments