1818"""
1919
2020import logging
21- from typing import Any , Dict , Optional , Sequence , Tuple , Union
21+ from typing import Dict , Optional , Tuple
2222
23+ from google .api_core import exceptions
2324from google .api_core .resumable_media import _common
2425
2526_LOGGER = logging .getLogger (__name__ )
@@ -80,7 +81,7 @@ def chunk_size(self) -> int:
8081 ) * self ._chunk_granularity
8182 return actual_chunk_size
8283
83- def initiate_request (
84+ def build_initiate_request (
8485 self ,
8586 stream_metadata : Optional [Dict [str , str ]] = None ,
8687 content_type : Optional [str ] = None ,
@@ -90,8 +91,8 @@ def initiate_request(
9091
9192 Args:
9293 stream_metadata (Optional[Dict[str, str]]): Additional headers for
93- the upload initiation request. These headers are ONLY applied to
94- the initial request and will NOT be included in subsequent chunk
94+ the upload initiation request. These headers are ONLY applied to
95+ the initial request and will NOT be included in subsequent chunk
9596 upload requests. If not specified, no additional headers will be appended.
9697 content_type (Optional[str]): MIME type of the uploaded content.
9798 If not specified, the `X-Goog-Upload-Header-Content-Type` header
@@ -147,6 +148,14 @@ def process_initiate_response(
147148 if granularity :
148149 self ._chunk_granularity = int (granularity )
149150
151+ def process_initiate_error (self , exc : Exception ) -> None :
152+ """Processes an error from the initiation request.
153+
154+ Args:
155+ exc (Exception): The exception raised during initiation.
156+ """
157+ self ._invalid = True
158+
150159 def build_chunk_request (
151160 self , data : bytes , final : bool = False
152161 ) -> Tuple [str , str , Dict [str , str ], bytes ]:
@@ -212,6 +221,23 @@ def process_chunk_response(
212221 elif status == _common .UploadStatus .CANCELLED :
213222 self ._invalid = True
214223
224+ def process_chunk_error (self , exc : Exception ) -> bool :
225+ """Processes an error from the chunk upload request.
226+
227+ Args:
228+ exc (Exception): The exception raised during chunk upload.
229+
230+ Returns:
231+ bool: True if the error is recoverable and requires a recovery query,
232+ False otherwise.
233+ """
234+ if isinstance (exc , exceptions .GoogleAPICallError ):
235+ if exc .code in _common .RECOVERABLE_STATUS_CODES :
236+ return True
237+
238+ self ._invalid = True
239+ return False
240+
215241 def build_recovery_request (self ) -> Tuple [str , str , Dict [str , str ], bytes ]:
216242 """Constructs a request to query the server's current upload state.
217243
@@ -255,3 +281,11 @@ def process_recovery_response(
255281 raise RuntimeError ("Upload was cancelled by server" )
256282
257283 return self ._bytes_uploaded
284+
285+ def process_recovery_error (self , exc : Exception ) -> None :
286+ """Processes an error from the recovery query request.
287+
288+ Args:
289+ exc (Exception): The exception raised during recovery.
290+ """
291+ self ._invalid = True
0 commit comments