Skip to content

Commit

Permalink
added use errors hook
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronknott committed Mar 31, 2023
1 parent 22249a2 commit 4b5d3c8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/provider/index.ts
Expand Up @@ -6,6 +6,7 @@ export * from './masa-context-provider';
export * from './masa-provider';
export * from './masa-query-client';
export * from './masa-shape';
export * from './use-errors';
export * from './use-debounce';
export * from './use-local-storage';
export * from './use-masa';
Expand Down
70 changes: 70 additions & 0 deletions src/provider/use-errors.ts
@@ -0,0 +1,70 @@
import { useState } from 'react';
import { SoulNameErrorCodes } from '@masa-finance/masa-sdk';

const Errors = {
arweave: {
title: 'Darn',
msg: 'One of our network providers experienced an issue. Please try again.',
icon: '💸',
},
network: {
title: 'Sorry',
msg: 'We experienced an issue when trying to confirm your transaction on the blockchain. Please try again.',
icon: '💸',
},
crypto: {
title: 'Sorry',
msg: 'It looks like the signing or verification of your transaction failed .Please try again.',
icon: '💸',
},
soulname: {
title: 'Whoops',
msg: 'Looks like that soul name is taken or not valid. Try a different name.',
icon: '💸',
},
unknown: {
title: 'Whoops',
msg: 'Something went wrong. Please try again.',
icon: '💸',
},
};

interface ErrorType {
title: string;
msg: string;
icon: string;
}

export const useErrors = () => {
const [error, setError] = useState<ErrorType | null>(null);

const handleErrors = (
errorCode: SoulNameErrorCodes = SoulNameErrorCodes.UnknownError
) => {
if (errorCode === null) return;
if (!(errorCode in SoulNameErrorCodes)) return;

switch (errorCode) {
case SoulNameErrorCodes.NoError:
setError(null);
break;
case SoulNameErrorCodes.ArweaveError:
setError(Errors.arweave);
break;
case SoulNameErrorCodes.NetworkError:
setError(Errors.network);
break;
case SoulNameErrorCodes.CryptoError:
setError(Errors.crypto);
break;
case SoulNameErrorCodes.SoulNameError:
setError(Errors.soulname);
break;
case SoulNameErrorCodes.UnknownError:
setError(Errors.unknown);
break;
}
};

return { handleErrors, error, SoulNameErrorCodes };
};

0 comments on commit 4b5d3c8

Please sign in to comment.