Closed alfangr closed 4 months ago
Hi,
I set a timeout for every fetch
. Will the issue persist if you increase it via fetchTimeout
?
I updated with this code, the problem still persists
export async function createFileInFolder(folderId: string, fileName: string, content: string) {
try {
const gdrive = await getGDrive();
const uploadResponse = gdrive.files.newMultipartUploader()
.setData(content, MimeTypes.TEXT)
.setRequestBody({
name: fileName,
parents: [folderId]
}).execute();
await withTimeout(uploadResponse, 30000);
console.log(`File "${fileName}" created successfully in folder with ID "${folderId}".`);
} catch (error) {
console.error(`Error creating file "${fileName}" in folder with ID "${folderId}".`, error);
throw error;
}
}
function withTimeout(promise: Promise<any>, ms: number): Promise<any> {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error("Operation timed out"));
}, ms);
promise
.then((res) => {
clearTimeout(timeout);
resolve(res);
})
.catch((err) => {
clearTimeout(timeout);
reject(err);
});
});
}
Please, consult the readme, use fetchTimeout
and see if it helps.
Owh sorry, works fine without any error catch. Thanks @RobinBobin
This is updated code
export async function createFileInFolder(folderId: string, fileName: string, content: string) {
try {
const gdrive = await getGDrive();
gdrive.fetchTimeout = 3000; <-- add this line
const uploadResponse = await gdrive.files.newMultipartUploader()
.setData(content, MimeTypes.TEXT)
.setRequestBody({
name: fileName,
parents: [folderId]
}).execute();
return uploadResponse;
} catch (error) {
console.error(`Error creating file "${fileName}" in folder with ID "${folderId}".`, error);
return null;
}
}
Glad you got it working 🎉
Btw, thanks for developing this library.
Actually I did create a folder and txt file to my gdrive it works fine, but why my code going to catch error and show
Error creating file "filename.txt" in folder with ID "folderId". [AbortError: Aborted]
. This is the code I used, am I missing something?