Skip to content

Commit

Permalink
chore: added error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth9890 committed Nov 16, 2023
1 parent d2d162f commit 3625838
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 57 deletions.
43 changes: 19 additions & 24 deletions src/pda/PDA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
UpdatePDAInput,
} from '../../.mesh';
import { PDAFilter, PDAStatus } from '../types';
import { errorHandler } from '../utils/errorHandler';

export class PDA {
private sdk: Sdk;
Expand All @@ -23,9 +24,8 @@ export class PDA {
async getPDA(id: string) {
try {
return await this.sdk.PDA_query({ id });
} catch (error: any) {
throw new Error(error?.message);
error;
} catch (error) {
throw new Error(errorHandler(error));
}
}

Expand All @@ -40,9 +40,8 @@ export class PDA {
async getPDACount(filter?: FilterPDAInput) {
try {
return (await this.sdk.PDACount_query({ filter })).PDACount;
} catch (error: any) {
throw new Error(error?.message);
error;
} catch (error) {
throw new Error(errorHandler(error));
}
}

Expand All @@ -55,9 +54,8 @@ export class PDA {
async getPDAs({ filter, order, skip, take }: PDAFilter) {
try {
return await this.sdk.PDAs_query({ filter, order, skip, take });
} catch (error: any) {
throw new Error(error?.message);
error;
} catch (error) {
throw new Error(errorHandler(error));
}
}

Expand All @@ -71,10 +69,8 @@ export class PDA {
async getIssuedPDAs({ filter, order, skip, take }: PDAFilter) {
try {
return await this.sdk.issuedPDAs_query({ filter, order, skip, take });
} catch (error: any) {
console.log(error);
throw new Error(error?.message);
error;
} catch (error) {
throw new Error(errorHandler(error));
}
}

Expand All @@ -88,9 +84,8 @@ export class PDA {
async getIssuedPDAsCount(filter?: FilterPDAInput) {
try {
return (await this.sdk.issuedPDAsCount_query({ filter })).issuedPDAsCount;
} catch (error: any) {
throw new Error(error?.message);
error;
} catch (error) {
throw new Error(errorHandler(error));
}
}

Expand All @@ -104,8 +99,8 @@ export class PDA {
async myPDACount(filter?: FilterPDAInput) {
try {
return (await this.sdk.myPDACount_query({ filter })).myPDACount;
} catch (error: any) {
throw new Error(error?.message);
} catch (error) {
throw new Error(errorHandler(error));
}
}

Expand All @@ -118,8 +113,8 @@ export class PDA {
async changePDAStatus({ id, status }: { id: string; status: PDAStatus }) {
try {
return await this.sdk.changePDAStatus_mutation({ input: { id, status } });
} catch (error: any) {
throw new Error(error?.message);
} catch (error) {
throw new Error(errorHandler(error));
}
}

Expand All @@ -132,8 +127,8 @@ export class PDA {
async createPDA(pdaInput: CreatePDAInput) {
try {
return await this.sdk.createPDA_mutation({ input: pdaInput });
} catch (error: any) {
throw new Error(error?.message);
} catch (error) {
throw new Error(errorHandler(error));
}
}

Expand All @@ -149,8 +144,8 @@ export class PDA {
async updatePDA(updatedPDA: UpdatePDAInput) {
try {
return await this.sdk.updatePDA_mutation({ input: updatedPDA });
} catch (error: any) {
throw new Error(error?.message);
} catch (error) {
throw new Error(errorHandler(error));
}
}
}
7 changes: 7 additions & 0 deletions src/utils/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const errorHandler = (error: any): string => {
if (typeof error === 'object' && error !== null && 'message' in error) {
return error.message;
} else {
return 'Something went wrong!';
}
};
54 changes: 21 additions & 33 deletions test/pda.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Gateway } from '../src/Gateway';
import { PDAStatus } from '../src/types';
import { PDAStatus, UserIdentifierType } from '../src/types';

const DEFAULT_TIMEOUT = 10000;

Expand All @@ -14,26 +14,26 @@ beforeAll(() => {
});

describe('PDA TESTING', () => {
// it(
// "create pda",
// async () => {
// let obj = {
// dataModelId: "f4014d53-b30f-4490-9812-cea379a1b398",
// description: "test",
// title: "test",
// claim: {
// gatewayUse: "test",
// },
// owner: {
// type: UserIdentifierType.EMAIL,
// value: "sid",
// },
// };
// const { createPDA } = await api.pda.createPDA(obj);
// expect(createPDA.dataAsset?.title).toEqual("test");
// },
// DEFAULT_TIMEOUT
// );
it(
'create pda',
async () => {
let obj = {
dataModelId: 'f4014d53-b30f-4490-9812-cea379a1b398',
description: 'test',
title: 'test',
claim: {
gatewayUse: 'test',
},
owner: {
type: UserIdentifierType.EMAIL,
value: 'sid',
},
};
const { createPDA } = await api.pda.createPDA(obj);
expect(createPDA.dataAsset?.title).toEqual('test');
},
DEFAULT_TIMEOUT,
);

// it(
// 'change pda status',
Expand Down Expand Up @@ -84,18 +84,6 @@ describe('PDA TESTING', () => {
DEFAULT_TIMEOUT,
);

// it(
// "issued pdas",
// async () => {
// const { issuedPDAs } = await api.pda.getIssuedPDAs({
// skip: 0,
// take: 10,
// });
// expect(issuedPDAs.length).toEqual(1);
// },
// DEFAULT_TIMEOUT
// );

it(
'issued pdas count',
async () => {
Expand Down

0 comments on commit 3625838

Please sign in to comment.