@@ -295,14 +295,14 @@ def errors(self):
295295 else :
296296 return []
297297
298- def load_backup (self , r ):
298+ def load_backup (self ):
299299 try :
300300 return resource_string (self .local_copy_fn )
301301 log .warn ("Got status={:d} while getting {}. Fallback to local copy." .format (r .status_code , self .url ))
302302 except IOError as ex :
303303 log .warn (
304304 "Caught an exception trying to load local backup for {} via {}: {}" .format (
305- r .url , self .local_copy_fn , ex
305+ self .url , self .local_copy_fn , ex
306306 )
307307 )
308308 return None
@@ -330,23 +330,25 @@ def load_resource(self, getter):
330330 data = r .text
331331 self .etag = r .headers .get ('ETag' , None ) or hex_digest (r .text , 'sha256' )
332332 elif self .local_copy_fn is not None :
333- data = self .load_backup (r )
333+ data = self .load_backup ()
334334 if data is not None and len (data ) > 0 :
335335 info ['Reason' ] = "Retrieved from local cache because status: {} != 200" .format (status )
336336 status = 218
337337
338338 info ['Status Code' ] = str (status )
339339
340340 except IOError as ex :
341- log .warn ("caught exception from {}: {}" .format (self .url , ex ))
342341 if self .local_copy_fn is not None :
343- data = self .load_backup (r )
342+ log .warn ("caught exception from {} - trying local backup: {}" .format (self .url , ex ))
343+ data = self .load_backup ()
344344 if data is not None and len (data ) > 0 :
345345 info ['Reason' ] = "Retrieved from local cache because exception: {}" .format (ex )
346346 status = 218
347+ if data is None or not len (data ) > 0 :
348+ raise ex # propagate exception if we can't find a backup
347349
348350 if data is None or not len (data ) > 0 :
349- raise ResourceException ("Got status={:d} while getting {} " .format (r . status_code , self .url ))
351+ raise ResourceException ("failed to fetch {} (status: {:d}) " .format (self .url , status ))
350352
351353 info ['State' ] = 'Fetched'
352354
@@ -359,7 +361,7 @@ def parse(self, getter):
359361 if parse_info is not None and isinstance (parse_info , dict ):
360362 info .update (parse_info )
361363
362- if status == 200 :
364+ if status != 218 : # write backup unless we just loaded from backup
363365 self .last_seen = utc_now ().replace (microsecond = 0 )
364366 safe_write (self .local_copy_fn , data , True )
365367
0 commit comments