8
8
from django .core .files .base import ContentFile , File
9
9
from django .core .files .storage import storages
10
10
11
+ from ietf .utils .log import log
12
+
11
13
12
14
# TODO-BLOBSTORE (Future, maybe after leaving 3.9) : add a return type
13
15
def _get_storage (kind : str ):
@@ -22,16 +24,21 @@ def _get_storage(kind: str):
22
24
23
25
def exists_in_storage (kind : str , name : str ) -> bool :
24
26
if settings .ENABLE_BLOBSTORAGE :
25
- store = _get_storage (kind )
26
- return store .exists_in_storage (kind , name )
27
- else :
28
- return False
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 )} " )
32
+ return False
29
33
30
34
31
35
def remove_from_storage (kind : str , name : str , warn_if_missing : bool = True ) -> None :
32
36
if settings .ENABLE_BLOBSTORAGE :
33
- store = _get_storage (kind )
34
- store .remove_from_storage (kind , name , warn_if_missing )
37
+ try :
38
+ store = _get_storage (kind )
39
+ store .remove_from_storage (kind , name , warn_if_missing )
40
+ except Exception as err :
41
+ log (f"Blobstore Error: Failed to remove { kind } :{ name } : { repr (err )} " )
35
42
return None
36
43
37
44
@@ -46,8 +53,11 @@ def store_file(
46
53
) -> None :
47
54
# debug.show('f"asked to store {name} into {kind}"')
48
55
if settings .ENABLE_BLOBSTORAGE :
49
- store = _get_storage (kind )
50
- store .store_file (kind , name , file , allow_overwrite , doc_name , doc_rev )
56
+ try :
57
+ store = _get_storage (kind )
58
+ store .store_file (kind , name , file , allow_overwrite , doc_name , doc_rev )
59
+ except Exception as err :
60
+ log (f"Blobstore Error: Failed to store file { kind } :{ name } : { repr (err )} " )
51
61
return None
52
62
53
63
@@ -60,7 +70,11 @@ def store_bytes(
60
70
doc_rev : Optional [str ] = None ,
61
71
) -> None :
62
72
if settings .ENABLE_BLOBSTORAGE :
63
- store_file (kind , name , ContentFile (content ), allow_overwrite )
73
+ try :
74
+ store_file (kind , name , ContentFile (content ), allow_overwrite )
75
+ except Exception as err :
76
+ # n.b., not likely to get an exception here because store_file or store_bytes will catch it
77
+ log (f"Blobstore Error: Failed to store bytes to { kind } :{ name } : { repr (err )} " )
64
78
return None
65
79
66
80
@@ -73,31 +87,41 @@ def store_str(
73
87
doc_rev : Optional [str ] = None ,
74
88
) -> None :
75
89
if settings .ENABLE_BLOBSTORAGE :
76
- content_bytes = content .encode ("utf-8" )
77
- store_bytes (kind , name , content_bytes , allow_overwrite )
90
+ try :
91
+ content_bytes = content .encode ("utf-8" )
92
+ store_bytes (kind , name , content_bytes , allow_overwrite )
93
+ except Exception as err :
94
+ # n.b., not likely to get an exception here because store_file or store_bytes will catch it
95
+ log (f"Blobstore Error: Failed to store string to { kind } :{ name } : { repr (err )} " )
78
96
return None
79
97
80
98
81
99
def retrieve_bytes (kind : str , name : str ) -> bytes :
82
100
from ietf .doc .storage_backends import maybe_log_timing
83
101
content = b""
84
102
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 ()
103
+ try :
104
+ store = _get_storage (kind )
105
+ with store .open (name ) as f :
106
+ with maybe_log_timing (
107
+ hasattr (store , "ietf_log_blob_timing" ) and store .ietf_log_blob_timing ,
108
+ "read" ,
109
+ bucket_name = store .bucket_name if hasattr (store , "bucket_name" ) else "" ,
110
+ name = name ,
111
+ ):
112
+ content = f .read ()
113
+ except Exception as err :
114
+ log (f"Blobstore Error: Failed to read bytes from { kind } :{ name } : { repr (err )} " )
94
115
return content
95
116
96
117
97
118
def retrieve_str (kind : str , name : str ) -> str :
98
119
content = ""
99
120
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" )
121
+ try :
122
+ content_bytes = retrieve_bytes (kind , name )
123
+ # TODO-BLOBSTORE: try to decode all the different ways doc.text() does
124
+ content = content_bytes .decode ("utf-8" )
125
+ except Exception as err :
126
+ log (f"Blobstore Error: Failed to read string from { kind } :{ name } : { repr (err )} " )
103
127
return content
0 commit comments