Skip to content

Commit

Permalink
fix: fetchWithGuard error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vraja-nayaka committed Nov 29, 2024
1 parent 2220248 commit 9ac5c72
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 8 deletions.
5 changes: 5 additions & 0 deletions idea/frontend/src/features/metadata/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const errorMessage = {
metadataNotFound: 'MetadataNotFound',
};

export { errorMessage };
4 changes: 2 additions & 2 deletions idea/frontend/src/features/metadata/hooks/use-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { HexString, ProgramMetadata } from '@gear-js/api';
import { useAlert } from '@gear-js/react-hooks';
import { useEffect, useMemo, useState } from 'react';

import { RPCError, RPCErrorCode } from '@/shared/services/rpcService';
import { useChain } from '@/hooks';
import { getLocalMetadata } from '@/features/local-indexer';

import { fetchMetadata } from '../api/requests';
import { errorMessage } from '../consts';

function useMetadata(hash?: HexString | null | undefined) {
const alert = useAlert();
Expand All @@ -30,7 +30,7 @@ function useMetadata(hash?: HexString | null | undefined) {

getMetadata(hash)
.then(({ result }) => result.hex && setMetadataHex(result.hex))
.catch(({ message, code }: RPCError) => code !== RPCErrorCode.MetadataNotFound && alert.error(message))
.catch(({ message }) => message !== errorMessage.metadataNotFound && alert.error(message))
.finally(() => setIsMetadataReady(true));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [hash]);
Expand Down
6 changes: 5 additions & 1 deletion idea/frontend/src/features/sails/api/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ const METHOD = {
GET_EVENTS: 'event.all',
} as const;

export { METHOD };
const errorMessage = {
sailsIdlNotFound: 'SailsIdlNotFound',
};

export { METHOD, errorMessage };
5 changes: 2 additions & 3 deletions idea/frontend/src/features/sails/hooks/use-sails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { useAlert } from '@gear-js/react-hooks';
import { useQuery } from '@tanstack/react-query';
import { useEffect } from 'react';

import { RPCError, RPCErrorCode } from '@/shared/services/rpcService';

import { getIdl } from '../api';
import { useSailsInit } from './use-sails-init';
import { errorMessage } from '../api/consts';

function useSails(codeId: HexString | null | undefined) {
const sails = useSailsInit();
Expand All @@ -29,7 +28,7 @@ function useSails(codeId: HexString | null | undefined) {

useEffect(() => {
if (!error) return;
if (error instanceof RPCError && error.code === RPCErrorCode.MetadataNotFound) return;
if (error.message === errorMessage.sailsIdlNotFound) return;

alert.error(error.message);
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
5 changes: 4 additions & 1 deletion idea/frontend/src/shared/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ const fetchWithGuard = async <T extends object>(...args: Parameters<typeof fetch
}
const response = await fetch(...args);

if (!response.ok) throw new Error(response.statusText);
if (!response.ok) {
const result = await response.json();
throw new Error('error' in result ? result.error : response.statusText);
}

return response.json() as T;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ enum RPCErrorCode {
ParseError = -32700,
InvalidRequest = -32600,
MethodNotFound = -32601,
MetadataNotFound = -32404,
InvalidParams = -32602,
InternalError = -32603,
}
Expand Down

0 comments on commit 9ac5c72

Please sign in to comment.