Skip to content

Commit

Permalink
refactor: implement error handles for common methods
Browse files Browse the repository at this point in the history
Refs: #25
  • Loading branch information
bucurdavid committed Sep 11, 2023
1 parent 5606d14 commit 0255cbe
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,15 @@ export class ErrFailedOperation extends Error {
super(`Failed to perform operation: ${method} : ${message}`);
}
}

export class ErrMissingTrait extends Error {
public constructor(trait: string) {
super(`Missing trait: ${trait}`);
}
}

export class ErrMissingValueForTrait extends Error {
public constructor(trait: string) {
super(`Missing value for trait: ${trait}`);
}
}
20 changes: 14 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import BigNumber from 'bignumber.js';
import { DataNft } from './datanft';
import { NftEnumType, NftType, Offer } from './interfaces';
import { ErrFetch } from './errors';
import {
ErrBadType,
ErrFetch,
ErrInvalidArgument,
ErrMissingTrait,
ErrMissingValueForTrait
} from './errors';

export function numberToPaddedHex(value: BigNumber.Value) {
let hex = new BigNumber(value).toString(16);
Expand Down Expand Up @@ -64,12 +70,14 @@ export async function checkTraitsUrl(traitsUrl: string) {
const traitsResponse = await fetch(traitsUrl);
const traits = await traitsResponse.json();

checkStatus(traitsResponse);

if (!traits.description) {
throw new Error('Traits description is empty');
throw new ErrMissingTrait(traits.description);
}

if (!Array.isArray(traits.attributes)) {
throw new Error('Traits attributes must be an array');
throw new ErrMissingTrait(traits.attributes);
}

const requiredTraits = ['Creator', 'Data Preview URL'];
Expand All @@ -81,13 +89,13 @@ export async function checkTraitsUrl(traitsUrl: string) {
(attribute: any) => attribute.trait_type === requiredTrait
)
) {
throw new Error(`Missing required trait: ${requiredTrait}`);
throw new ErrMissingTrait(requiredTrait);
}
}

for (const attribute of traitsAttributes) {
if (!attribute.value) {
throw new Error(`Empty value for trait: ${attribute.trait_type}`);
throw new ErrMissingValueForTrait(attribute.trait_type);
}
}
}
Expand Down Expand Up @@ -533,7 +541,7 @@ export async function checkUrlIsUp(url: string, expectedHttpCodes: number[]) {
}

export function checkStatus(response: Response) {
if (response.status >= 200 && response.status <= 299) {
if (!(response.status >= 200 && response.status <= 299)) {
throw new ErrFetch(response.status, response.statusText);
}
}
4 changes: 2 additions & 2 deletions tests/traits-check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ describe('Traits strucutre test', () => {
await checkTraitsUrl(
'https://ipfs.io/ipfs/bafybeicbmpiehja5rjk425ol4rmrorrg5xh62vcbeqigv3zjcrfk4rtggm/metadata.json'
);
} catch (error) {
expect(error).toStrictEqual(Error('Missing required trait: Creator'));
} catch (error: any) {
expect(error.message).toBe('Missing trait: Creator');
}
}, 100000);
});

0 comments on commit 0255cbe

Please sign in to comment.