Skip to content

Commit 2f09b61

Browse files
committed
fix: improve permission insertion error handling in shareFile function
1 parent 093034a commit 2f09b61

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

drive/snippets/drive_v2/file snippets/share_file.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,17 @@ async function shareFile(fileId, targetUser, targetDomain) {
5454
// Note: The client library does not currently support batch requests for permissions.
5555
// When possible, use batch requests to insert multiple permissions on the same item.
5656
for (const permission of permissions) {
57-
try {
58-
// Insert the permission.
59-
const result = await service.permissions.insert({
60-
requestBody: permission,
61-
fileId,
62-
fields: 'id',
63-
});
57+
// Insert the permission.
58+
const result = await service.permissions.insert({
59+
requestBody: permission,
60+
fileId,
61+
fields: 'id',
62+
});
63+
if (result.data.id) {
6464
permissionIds.push(result.data.id);
6565
console.log(`Inserted permission id: ${result.data.id}`);
66-
} catch (err) {
67-
// TODO(developer): Handle failed permissions
68-
console.error(err);
66+
} else {
67+
throw new Error('Failed to create permission');
6968
}
7069
}
7170
return permissionIds;

drive/snippets/drive_v3/file_snippets/share_file.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ async function shareFile(fileId, targetUserEmail, targetDomainName) {
6161
fields: 'id',
6262
});
6363

64-
if (!result.data.id) {
64+
if (result.data.id) {
65+
permissionIds.push(result.data.id);
66+
console.log(`Inserted permission id: ${result.data.id}`);
67+
} else {
6568
throw new Error('Failed to create permission');
6669
}
67-
68-
permissionIds.push(result.data.id);
69-
console.log(`Inserted permission id: ${result.data.id}`);
7070
}
7171
return permissionIds;
7272
}

0 commit comments

Comments
 (0)