Skip to content

Commit

Permalink
Merge pull request #236 from ardriveapp/PE-6152-catch-gql-errors-in-r…
Browse files Browse the repository at this point in the history
…esponse

chore: release 2.0.3
  • Loading branch information
fedellen authored May 20, 2024
2 parents 0958c8f + b67ced3 commit bd6f0d1
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 292 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ardrive-core-js",
"version": "2.0.2",
"version": "2.0.3",
"description": "ArDrive Core contains the essential back end application features to support the ArDrive CLI and Desktop apps, such as file management, Permaweb upload/download, wallet management and other common functions.",
"main": "./lib/exports.js",
"types": "./lib/exports.d.ts",
Expand Down
79 changes: 25 additions & 54 deletions src/ardrive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,7 @@ export class ArDrive extends ArDriveAnonymous {
public async movePublicFile({ fileId, newParentFolderId }: MovePublicFileParams): Promise<ArFSResult> {
const destFolderDriveId = await this.arFsDao.getDriveIdForFolderId(newParentFolderId);

const owner = await this.getOwnerForDriveId(destFolderDriveId);
await this.assertOwnerAddress(owner);

const owner = await this.wallet.getAddress();
const originalFileMetaData = await this.getPublicFile({ fileId });

if (!destFolderDriveId.equals(originalFileMetaData.driveId)) {
Expand Down Expand Up @@ -256,9 +254,7 @@ export class ArDrive extends ArDriveAnonymous {
public async movePrivateFile({ fileId, newParentFolderId, driveKey }: MovePrivateFileParams): Promise<ArFSResult> {
const destFolderDriveId = await this.arFsDao.getDriveIdForFolderId(newParentFolderId);

const owner = await this.getOwnerForDriveId(destFolderDriveId);
await this.assertOwnerAddress(owner);

const owner = await this.wallet.getAddress();
const originalFileMetaData = await this.getPrivateFile({ fileId, driveKey });

if (!destFolderDriveId.equals(originalFileMetaData.driveId)) {
Expand Down Expand Up @@ -345,9 +341,7 @@ export class ArDrive extends ArDriveAnonymous {

const destFolderDriveId = await this.arFsDao.getDriveIdForFolderId(newParentFolderId);

const owner = await this.getOwnerForDriveId(destFolderDriveId);
await this.assertOwnerAddress(owner);

const owner = await this.wallet.getAddress();
const originalFolderMetaData = await this.getPublicFolder({ folderId });

if (!destFolderDriveId.equals(originalFolderMetaData.driveId)) {
Expand Down Expand Up @@ -430,9 +424,7 @@ export class ArDrive extends ArDriveAnonymous {

const destFolderDriveId = await this.arFsDao.getDriveIdForFolderId(newParentFolderId);

const owner = await this.getOwnerForDriveId(destFolderDriveId);
await this.assertOwnerAddress(owner);

const owner = await this.wallet.getAddress();
const originalFolderMetaData = await this.getPrivateFolder({ folderId, driveKey });

if (!destFolderDriveId.equals(originalFolderMetaData.driveId)) {
Expand Down Expand Up @@ -594,13 +586,10 @@ export class ArDrive extends ArDriveAnonymous {
const preparedEntities: UploadStats[] = [];

for (const entity of entitiesToUpload) {
const { destFolderId, driveKey } = entity;
const { destFolderId } = entity;
const destDriveId = await this.arFsDao.getDriveIdForFolderId(destFolderId);

// Assert drive privacy and owner of the drive
const owner = await this.arFsDao.getOwnerAndAssertDrive(destDriveId, driveKey);
await this.assertOwnerAddress(owner);

const owner = await this.wallet.getAddress();
preparedEntities.push({ ...entity, destDriveId, owner });
}

Expand Down Expand Up @@ -822,7 +811,7 @@ export class ArDrive extends ArDriveAnonymous {
}

private async deriveMetaDataTxIdForFileId(fileId: FileID, dataTxId: TransactionID): Promise<TransactionID> {
const owner = await this.arFsDao.getDriveOwnerForFileId(fileId);
const owner = await this.wallet.getAddress();
const fileMetaData = await this.arFsDao.getPublicFile(fileId, owner);

if (fileMetaData.dataTxId.equals(dataTxId)) {
Expand All @@ -837,7 +826,7 @@ export class ArDrive extends ArDriveAnonymous {
destinationFolderId: FolderID,
dataTxId: TransactionID
): Promise<ArFSPublicFile | undefined> {
const owner = await this.arFsDao.getDriveOwnerForFolderId(destinationFolderId);
const owner = await this.wallet.getAddress();
await this.assertFolderExists(destinationFolderId, owner);

const allFileMetaDataTxInFolder = await this.arFsDao.getPublicFilesWithParentFolderIds(
Expand Down Expand Up @@ -870,10 +859,7 @@ export class ArDrive extends ArDriveAnonymous {
destinationFolderId: FolderID;
conflictResolution: FileNameConflictResolution;
}): Promise<boolean> {
const destDriveId = await this.arFsDao.getDriveIdForFolderId(destinationFolderId);
const owner = await this.arFsDao.getOwnerAndAssertDrive(destDriveId);
await this.assertOwnerAddress(owner);

const owner = await this.wallet.getAddress();
await resolveFileNameConflicts({
wrappedFile,
conflictResolution,
Expand Down Expand Up @@ -989,12 +975,8 @@ export class ArDrive extends ArDriveAnonymous {
conflictResolution = upsertOnConflicts,
prompts
}: UploadPublicManifestParams): Promise<ArFSManifestResult> {
const driveId = await this.arFsDao.getDriveIdForFolderId(folderId);

// Assert that the owner of this drive is consistent with the provided wallet
const owner = await this.getOwnerForDriveId(driveId);
await this.assertOwnerAddress(owner);

const owner = await this.wallet.getAddress();
const children = await this.listPublicFolder({
folderId,
maxDepth,
Expand Down Expand Up @@ -1034,8 +1016,7 @@ export class ArDrive extends ArDriveAnonymous {
assertValidArFSFolderName(folderName);

const driveId = await this.arFsDao.getDriveIdForFolderId(parentFolderId);
const owner = await this.arFsDao.getOwnerAndAssertDrive(driveId);
await this.assertOwnerAddress(owner);
const owner = await this.wallet.getAddress();

// Assert that there are no duplicate names in the destination folder
const entityNamesInParentFolder = await this.arFsDao.getPublicEntityNamesInFolder(parentFolderId, owner);
Expand Down Expand Up @@ -1096,8 +1077,7 @@ export class ArDrive extends ArDriveAnonymous {
assertValidArFSFolderName(folderName);

const driveId = await this.arFsDao.getDriveIdForFolderId(parentFolderId);
const owner = await this.arFsDao.getOwnerAndAssertDrive(driveId, driveKey);
await this.assertOwnerAddress(owner);
const owner = await this.wallet.getAddress();

// Assert that there are no duplicate names in the destination folder
const entityNamesInParentFolder = await this.arFsDao.getPrivateEntityNamesInFolder(
Expand Down Expand Up @@ -1286,10 +1266,8 @@ export class ArDrive extends ArDriveAnonymous {
withKeys
}: GetPrivateDriveParams): Promise<ArFSPrivateDrive> {
if (!owner) {
owner = await this.getOwnerForDriveId(driveId);
owner = await this.wallet.getAddress();
}
await this.assertOwnerAddress(owner);

const drive = await this.arFsDao.getPrivateDrive(driveId, driveKey, owner);
return withKeys
? drive
Expand Down Expand Up @@ -1318,9 +1296,8 @@ export class ArDrive extends ArDriveAnonymous {
withKeys
}: GetPrivateFolderParams): Promise<ArFSPrivateFolder> {
if (!owner) {
owner = await this.arFsDao.getDriveOwnerForFolderId(folderId);
owner = await this.wallet.getAddress();
}
await this.assertOwnerAddress(owner);

const folder = await this.arFsDao.getPrivateFolder(folderId, driveKey, owner);
return withKeys ? folder : new ArFSPrivateFolderKeyless(folder);
Expand All @@ -1343,7 +1320,7 @@ export class ArDrive extends ArDriveAnonymous {
withKeys = false
}: GetPrivateFileParams): Promise<ArFSPrivateFile> {
if (!owner) {
owner = await this.arFsDao.getDriveOwnerForFileId(fileId);
owner = await this.wallet.getAddress();
}

const file = await this.arFsDao.getPrivateFile(fileId, driveKey, owner);
Expand All @@ -1364,9 +1341,8 @@ export class ArDrive extends ArDriveAnonymous {
withKeys = false
}: ListPrivateFolderParams): Promise<(ArFSPrivateFolderWithPaths | ArFSPrivateFileWithPaths)[]> {
if (!owner) {
owner = await this.arFsDao.getDriveOwnerForFolderId(folderId);
owner = await this.wallet.getAddress();
}
await this.assertOwnerAddress(owner);

const withPathsFactory = withKeys ? privateEntityWithPathsFactory : privateEntityWithPathsKeylessFactory;

Expand Down Expand Up @@ -1490,8 +1466,8 @@ export class ArDrive extends ArDriveAnonymous {
}

async renamePublicFile({ fileId, newName }: RenamePublicFileParams): Promise<ArFSResult> {
const owner = await this.arFsDao.getDriveOwnerForFileId(fileId);
await this.assertOwnerAddress(owner);
const owner = await this.wallet.getAddress();

const file = await this.getPublicFile({ fileId, owner });
if (file.name === newName) {
throw new Error(`To rename a file, the new name must be different`);
Expand Down Expand Up @@ -1549,8 +1525,7 @@ export class ArDrive extends ArDriveAnonymous {
}

async renamePrivateFile({ fileId, newName, driveKey }: RenamePrivateFileParams): Promise<ArFSResult> {
const owner = await this.arFsDao.getDriveOwnerForFileId(fileId);
await this.assertOwnerAddress(owner);
const owner = await this.wallet.getAddress();
const file = await this.getPrivateFile({ fileId, driveKey, owner });
if (file.name === newName) {
throw new Error(`To rename a file, the new name must be different`);
Expand Down Expand Up @@ -1613,8 +1588,7 @@ export class ArDrive extends ArDriveAnonymous {
}

async renamePublicFolder({ folderId, newName }: RenamePublicFolderParams): Promise<ArFSResult> {
const owner = await this.arFsDao.getDriveOwnerForFolderId(folderId);
await this.assertOwnerAddress(owner);
const owner = await this.wallet.getAddress();
const folder = await this.getPublicFolder({ folderId, owner });
if (`${folder.parentFolderId}` === ROOT_FOLDER_ID_PLACEHOLDER) {
throw new Error(
Expand Down Expand Up @@ -1671,8 +1645,7 @@ export class ArDrive extends ArDriveAnonymous {
}

async renamePrivateFolder({ folderId, newName, driveKey }: RenamePrivateFolderParams): Promise<ArFSResult> {
const owner = await this.arFsDao.getDriveOwnerForFolderId(folderId);
await this.assertOwnerAddress(owner);
const owner = await this.wallet.getAddress();
const folder = await this.getPrivateFolder({ folderId, driveKey, owner });
if (`${folder.parentFolderId}` === ROOT_FOLDER_ID_PLACEHOLDER) {
throw new Error(
Expand Down Expand Up @@ -1735,8 +1708,7 @@ export class ArDrive extends ArDriveAnonymous {
}

async renamePublicDrive({ driveId, newName }: RenamePublicDriveParams): Promise<ArFSResult> {
const owner = await this.arFsDao.getOwnerForDriveId(driveId);
await this.assertOwnerAddress(owner);
const owner = await this.wallet.getAddress();
const drive = await this.getPublicDrive({ driveId, owner });
if (drive.name === newName) {
throw new Error(`New drive name '${newName}' must be different from the current drive name!`);
Expand Down Expand Up @@ -1791,8 +1763,7 @@ export class ArDrive extends ArDriveAnonymous {
}

async renamePrivateDrive({ driveId, newName, driveKey }: RenamePrivateDriveParams): Promise<ArFSResult> {
const owner = await this.arFsDao.getOwnerForDriveId(driveId);
await this.assertOwnerAddress(owner);
const owner = await this.wallet.getAddress();
const drive = await this.getPrivateDrive({ driveId, owner, driveKey });
if (drive.name === newName) {
throw new Error(`New drive name '${newName}' must be different from the current drive name!`);
Expand Down Expand Up @@ -1858,7 +1829,7 @@ export class ArDrive extends ArDriveAnonymous {
owner
}: DownloadPrivateFolderParameters): Promise<void> {
if (!owner) {
owner = await this.arFsDao.getDriveOwnerForFolderId(folderId);
owner = await this.wallet.getAddress();
}

return this.arFsDao.downloadPrivateFolder({
Expand All @@ -1880,7 +1851,7 @@ export class ArDrive extends ArDriveAnonymous {
owner
}: DownloadPrivateDriveParameters): Promise<void> {
if (!owner) {
owner = await this.arFsDao.getOwnerForDriveId(driveId);
owner = await this.wallet.getAddress();
}

const drive = await this.arFsDao.getPrivateDrive(driveId, driveKey, owner);
Expand Down
3 changes: 1 addition & 2 deletions src/arfs/arfsdao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ import {
W,
GQLEdgeInterface,
GQLNodeInterface,
DrivePrivacy,
DriveID,
DriveKey,
FolderID,
Expand Down Expand Up @@ -176,7 +175,7 @@ import {
} from './tx/arfs_tx_data_types';
import { ArFSTagAssembler } from './tags/tag_assembler';
import { assertDataRootsMatch, rePrepareV2Tx } from '../utils/arfsdao_utils';
import { ArFSDataToUpload, ArFSFolderToUpload } from '../exports';
import { ArFSDataToUpload, ArFSFolderToUpload, DrivePrivacy } from '../exports';
import { Turbo, TurboCachesResponse } from './turbo';
import { ArweaveSigner } from 'arbundles/src/signing';

Expand Down
36 changes: 33 additions & 3 deletions src/types/entity_key.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,47 @@ describe('EntityKey class', () => {

it('throws if a non-buffer is given', () => {
// eslint-disable-next-line prettier/prettier
const nonBuffer: Buffer = 'non buffer type' as unknown as Buffer;
const nonBuffer: Buffer = ('non buffer type' as unknown) as Buffer;
expect(() => new EntityKey(nonBuffer)).to.throw('The argument must be of type Buffer, got string');
});

it('the original buffer contains the correct data', async () => {
expect(key.keyData).to.deep.equal(
Buffer.from([
// eslint-disable-next-line prettier/prettier
159, 20, 229, 218, 72, 185, 133, 104, 242, 96, 77, 18, 140, 232, 54, 21, 93, 207, 19, 177, 1, 40, 199,
159,
20,
229,
218,
72,
185,
133,
104,
242,
96,
77,
18,
140,
232,
54,
21,
93,
207,
19,
177,
1,
40,
199,
// eslint-disable-next-line prettier/prettier
189, 19, 169, 3, 242, 227, 175, 155, 172
189,
19,
169,
3,
242,
227,
175,
155,
172
])
);
});
Expand Down
10 changes: 9 additions & 1 deletion src/types/gql_Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,13 @@ export interface GQLTransactionsResultInterface {
export default interface GQLResultInterface {
data: {
transactions: GQLTransactionsResultInterface;
};
} | null;
errors:
| {
message: string;
path: string[];
locations: { line: number; column: number }[];
extensions: { code: string };
}[]
| undefined;
}
18 changes: 18 additions & 0 deletions src/utils/gateway_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ export class GatewayAPI {
try {
const { data } = await this.postToEndpoint<GQLResultInterface>('graphql', query);

if (data.errors) {
data.errors.forEach((error) => {
console.error(`GQL Error: ${error.message}`);
});
}

if (!data.data) {
const isTimeoutError = data.errors?.some((error) =>
error.message.includes('canceling statement due to statement timeout')
);

if (isTimeoutError) {
throw new Error('GQL Query has been timed out.');
}

throw new Error('No data was returned from the GQL request.');
}

return data.data.transactions;
} catch (error) {
throw Error(`GQL Error: ${(error as Error).message}`);
Expand Down
Loading

0 comments on commit bd6f0d1

Please sign in to comment.