mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-14 17:07:25 +00:00
add error message to the google drive document loader (#2186)
When downloading a google doc, if the document is not a google doc type, for example if you uploaded a .DOCX file to your google drive, the error you get is not informative at all. I added a error handler which print the exact error occurred during downloading the document from google docs.
This commit is contained in:
parent
2eeaccf01c
commit
6c66f51fb8
@ -142,6 +142,7 @@ class GoogleDriveLoader(BaseLoader, BaseModel):
|
|||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
from googleapiclient.discovery import build
|
from googleapiclient.discovery import build
|
||||||
|
from googleapiclient.errors import HttpError
|
||||||
from googleapiclient.http import MediaIoBaseDownload
|
from googleapiclient.http import MediaIoBaseDownload
|
||||||
|
|
||||||
creds = self._load_credentials()
|
creds = self._load_credentials()
|
||||||
@ -151,8 +152,16 @@ class GoogleDriveLoader(BaseLoader, BaseModel):
|
|||||||
fh = BytesIO()
|
fh = BytesIO()
|
||||||
downloader = MediaIoBaseDownload(fh, request)
|
downloader = MediaIoBaseDownload(fh, request)
|
||||||
done = False
|
done = False
|
||||||
while done is False:
|
try:
|
||||||
status, done = downloader.next_chunk()
|
while done is False:
|
||||||
|
status, done = downloader.next_chunk()
|
||||||
|
|
||||||
|
except HttpError as e:
|
||||||
|
if e.resp.status == 404:
|
||||||
|
print("File not found: {}".format(id))
|
||||||
|
else:
|
||||||
|
print("An error occurred: {}".format(e))
|
||||||
|
|
||||||
text = fh.getvalue().decode("utf-8")
|
text = fh.getvalue().decode("utf-8")
|
||||||
metadata = {"source": f"https://docs.google.com/document/d/{id}/edit"}
|
metadata = {"source": f"https://docs.google.com/document/d/{id}/edit"}
|
||||||
return Document(page_content=text, metadata=metadata)
|
return Document(page_content=text, metadata=metadata)
|
||||||
|
Loading…
Reference in New Issue
Block a user