Skip to content

Commit e180a58

Browse files
committed
MOD: Streaming exceptions to BentoError
1 parent 89abe36 commit e180a58

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

databento/historical/api/batch.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from databento.common.enums import Delivery
2121
from databento.common.enums import Packaging
2222
from databento.common.enums import SplitDuration
23+
from databento.common.error import BentoError
2324
from databento.common.parsing import datetime_to_string
2425
from databento.common.parsing import optional_datetime_to_string
2526
from databento.common.parsing import optional_symbols_list_to_list
@@ -371,8 +372,11 @@ def _download_file(
371372

372373
logger.debug("Starting download of file %s", output_path.name)
373374
with open(output_path, mode=mode) as f:
374-
for chunk in response.iter_content(chunk_size=None):
375-
f.write(chunk)
375+
try:
376+
for chunk in response.iter_content(chunk_size=None):
377+
f.write(chunk)
378+
except Exception as exc:
379+
raise BentoError(f"Error downloading file: {exc}")
376380
logger.debug("Download of %s completed", output_path.name)
377381

378382
async def download_async(
@@ -512,9 +516,12 @@ async def _download_file_async(
512516

513517
logger.debug("Starting async download of file %s", output_path.name)
514518
with open(output_path, mode=mode) as f:
515-
async for chunk in response.content.iter_chunks():
516-
data: bytes = chunk[0]
517-
f.write(data)
519+
try:
520+
async for chunk in response.content.iter_chunks():
521+
data: bytes = chunk[0]
522+
f.write(data)
523+
except Exception as exc:
524+
raise BentoError(f"Error downloading file: {exc}")
518525
logger.debug("Download of %s completed", output_path.name)
519526

520527
def _get_file_download_headers_and_mode(

databento/historical/http.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from databento.common.dbnstore import DBNStore
1919
from databento.common.error import BentoClientError
2020
from databento.common.error import BentoDeprecationWarning
21+
from databento.common.error import BentoError
2122
from databento.common.error import BentoServerError
2223
from databento.common.error import BentoWarning
2324
from databento.common.system import USER_AGENT
@@ -132,8 +133,11 @@ def _stream(
132133
else:
133134
writer = open(path, "x+b")
134135

135-
for chunk in response.iter_content(chunk_size=None):
136-
writer.write(chunk)
136+
try:
137+
for chunk in response.iter_content(chunk_size=None):
138+
writer.write(chunk)
139+
except Exception as exc:
140+
raise BentoError(f"Error streaming response: {exc}")
137141

138142
if path is None:
139143
writer.seek(0)
@@ -169,8 +173,11 @@ async def _stream_async(
169173
else:
170174
writer = open(path, "x+b")
171175

172-
async for chunk in response.content.iter_chunks():
173-
writer.write(chunk[0])
176+
try:
177+
async for chunk in response.content.iter_chunks():
178+
writer.write(chunk[0])
179+
except Exception as exc:
180+
raise BentoError(f"Error streaming response: {exc}")
174181

175182
if path is None:
176183
writer.seek(0)

0 commit comments

Comments
 (0)