RobinBobin / react-native-google-drive-api-wrapper

This wrapper facilitates the use of the Google Drive API in React Native projects.
110 stars 43 forks source link

Success upload file but still going to error catch #100

Closed alfangr closed 4 months ago

alfangr commented 4 months ago

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?

export async function createFileInFolder(folderId: string, fileName: string, content: string) {
  try {
    const gdrive = await getGDrive();
    await gdrive.files.newMultipartUploader()
      .setData(content, MimeTypes.TEXT)
      .setRequestBody({
        name: fileName,
        parents: [folderId]
      }).execute();
  } catch (error) {
    console.error(`Error creating file "${fileName}" in folder with ID "${folderId}".`, error);
    throw error;
  }
}
RobinBobin commented 4 months ago

Hi, I set a timeout for every fetch. Will the issue persist if you increase it via fetchTimeout?

alfangr commented 4 months ago

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);
      });
  });
}
RobinBobin commented 4 months ago

Please, consult the readme, use fetchTimeout and see if it helps.

alfangr commented 4 months ago

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;
  }
}
RobinBobin commented 4 months ago

Glad you got it working 🎉