From 0255cbe10782c92c0ce7978dd2204457e30f2c52 Mon Sep 17 00:00:00 2001 From: Bucur David Date: Mon, 11 Sep 2023 13:41:43 +0300 Subject: [PATCH] refactor: implement error handles for common methods Refs: #25 --- src/errors.ts | 12 ++++++++++++ src/utils.ts | 20 ++++++++++++++------ tests/traits-check.test.ts | 4 ++-- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/errors.ts b/src/errors.ts index 7d644cc..1ec6864 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -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}`); + } +} diff --git a/src/utils.ts b/src/utils.ts index 35fbb49..959034a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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); @@ -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']; @@ -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); } } } @@ -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); } } diff --git a/tests/traits-check.test.ts b/tests/traits-check.test.ts index 4f40f59..7f0d8c9 100644 --- a/tests/traits-check.test.ts +++ b/tests/traits-check.test.ts @@ -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); });