88from django .core .files .base import ContentFile , File
99from django .core .files .storage import storages
1010
11+ from ietf .utils .log import log
12+
1113
1214# TODO-BLOBSTORE (Future, maybe after leaving 3.9) : add a return type
1315def _get_storage (kind : str ):
@@ -22,16 +24,22 @@ def _get_storage(kind: str):
2224
2325def exists_in_storage (kind : str , name : str ) -> bool :
2426 if settings .ENABLE_BLOBSTORAGE :
25- store = _get_storage (kind )
26- return store .exists_in_storage (kind , name )
27+ try :
28+ store = _get_storage (kind )
29+ return store .exists_in_storage (kind , name )
30+ except Exception as err :
31+ log (f"Blobstore Error: Failed to test existence of { kind } :{ name } : { repr (err )} " )
2732 else :
2833 return False
2934
3035
3136def remove_from_storage (kind : str , name : str , warn_if_missing : bool = True ) -> None :
3237 if settings .ENABLE_BLOBSTORAGE :
33- store = _get_storage (kind )
34- store .remove_from_storage (kind , name , warn_if_missing )
38+ try :
39+ store = _get_storage (kind )
40+ store .remove_from_storage (kind , name , warn_if_missing )
41+ except Exception as err :
42+ log (f"Blobstore Error: Failed to remove { kind } :{ name } : { repr (err )} " )
3543 return None
3644
3745
@@ -46,8 +54,11 @@ def store_file(
4654) -> None :
4755 # debug.show('f"asked to store {name} into {kind}"')
4856 if settings .ENABLE_BLOBSTORAGE :
49- store = _get_storage (kind )
50- store .store_file (kind , name , file , allow_overwrite , doc_name , doc_rev )
57+ try :
58+ store = _get_storage (kind )
59+ store .store_file (kind , name , file , allow_overwrite , doc_name , doc_rev )
60+ except Exception as err :
61+ log (f"Blobstore Error: Failed to store file { kind } :{ name } : { repr (err )} " )
5162 return None
5263
5364
@@ -60,7 +71,11 @@ def store_bytes(
6071 doc_rev : Optional [str ] = None ,
6172) -> None :
6273 if settings .ENABLE_BLOBSTORAGE :
63- store_file (kind , name , ContentFile (content ), allow_overwrite )
74+ try :
75+ store_file (kind , name , ContentFile (content ), allow_overwrite )
76+ except Exception as err :
77+ # n.b., not likely to get an exception here because store_file or store_bytes will catch it
78+ log (f"Blobstore Error: Failed to store bytes to { kind } :{ name } : { repr (err )} " )
6479 return None
6580
6681
@@ -73,31 +88,41 @@ def store_str(
7388 doc_rev : Optional [str ] = None ,
7489) -> None :
7590 if settings .ENABLE_BLOBSTORAGE :
76- content_bytes = content .encode ("utf-8" )
77- store_bytes (kind , name , content_bytes , allow_overwrite )
91+ try :
92+ content_bytes = content .encode ("utf-8" )
93+ store_bytes (kind , name , content_bytes , allow_overwrite )
94+ except Exception as err :
95+ # n.b., not likely to get an exception here because store_file or store_bytes will catch it
96+ log (f"Blobstore Error: Failed to store string to { kind } :{ name } : { repr (err )} " )
7897 return None
7998
8099
81100def retrieve_bytes (kind : str , name : str ) -> bytes :
82101 from ietf .doc .storage_backends import maybe_log_timing
83102 content = b""
84103 if settings .ENABLE_BLOBSTORAGE :
85- store = _get_storage (kind )
86- with store .open (name ) as f :
87- with maybe_log_timing (
88- hasattr (store , "ietf_log_blob_timing" ) and store .ietf_log_blob_timing ,
89- "read" ,
90- bucket_name = store .bucket_name if hasattr (store , "bucket_name" ) else "" ,
91- name = name ,
92- ):
93- content = f .read ()
104+ try :
105+ store = _get_storage (kind )
106+ with store .open (name ) as f :
107+ with maybe_log_timing (
108+ hasattr (store , "ietf_log_blob_timing" ) and store .ietf_log_blob_timing ,
109+ "read" ,
110+ bucket_name = store .bucket_name if hasattr (store , "bucket_name" ) else "" ,
111+ name = name ,
112+ ):
113+ content = f .read ()
114+ except Exception as err :
115+ log (f"Blobstore Error: Failed to read bytes from { kind } :{ name } : { repr (err )} " )
94116 return content
95117
96118
97119def retrieve_str (kind : str , name : str ) -> str :
98120 content = ""
99121 if settings .ENABLE_BLOBSTORAGE :
100- content_bytes = retrieve_bytes (kind , name )
101- # TODO-BLOBSTORE: try to decode all the different ways doc.text() does
102- content = content_bytes .decode ("utf-8" )
122+ try :
123+ content_bytes = retrieve_bytes (kind , name )
124+ # TODO-BLOBSTORE: try to decode all the different ways doc.text() does
125+ content = content_bytes .decode ("utf-8" )
126+ except Exception as err :
127+ log (f"Blobstore Error: Failed to read string from { kind } :{ name } : { repr (err )} " )
103128 return content
0 commit comments