Skip to content

Commit

Permalink
Handle playHandsign and claimInactivity errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lindlof committed Dec 13, 2020
1 parent 6231ff5 commit a63012c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
45 changes: 41 additions & 4 deletions web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const App: React.FC = () => {
playHandsign(client, game, handsign, setGame, enqueueSnackbar)
}
leaveGame={() => setGame(undefined)}
claimInactivity={async () => setGame(await Game.claimInactivity(client, game))}
claimInactivity={() => claimInactivity(client, game, setGame, enqueueSnackbar)}
enqueueSnackbar={enqueueSnackbar}
/>
</GameTicker>
Expand Down Expand Up @@ -123,7 +123,6 @@ const playGame = async (
return;
}
if (e.message.includes('Error when posting tx ')) {
console.log('playGame error:', e.message);
enqueueSnackbar('Error posting transaction', { variant: 'error' });
setGame(undefined);
return;
Expand Down Expand Up @@ -157,9 +156,47 @@ const playHandsign = async (
setGame((g: Game.Game) => ({ ...g, lastHandsign: handsign }));
try {
await Game.playHandsign(client, game, handsign);
} catch (error) {
} catch (e) {
setGame((g: Game.Game) => ({ ...g, lastHandsign: undefined }));
enqueueSnackbar('Secret error', { variant: 'error' });
if (e instanceof Error) {
if (e.message === 'Request rejected') {
enqueueSnackbar('Transaction rejected', { variant: 'error' });
throw e;
}
if (e.message.includes('Error when posting tx ')) {
enqueueSnackbar('Error posting transaction', { variant: 'error' });
throw e;
}
}
console.log('playHandsign error:', e.message);
enqueueSnackbar('Error playing handsign', { variant: 'error' });
throw e;
}
};

const claimInactivity = async (
client: SecretJS.SigningCosmWasmClient,
game: Game.Game,
setGame: Function,
enqueueSnackbar: Function,
) => {
try {
await Game.claimInactivity(client, game);
setGame(undefined);
} catch (e) {
if (e instanceof Error) {
if (e.message === 'Request rejected') {
enqueueSnackbar('Transaction rejected', { variant: 'error' });
throw e;
}
if (e.message.includes('Error when posting tx ')) {
enqueueSnackbar('Error posting transaction', { variant: 'error' });
throw e;
}
}
console.log('claimInactivity error:', e.message);
enqueueSnackbar('Error ending game', { variant: 'error' });
throw e;
}
};

Expand Down
11 changes: 7 additions & 4 deletions web/src/GamePlaying.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const useStyles = makeStyles((theme) => ({

interface Props {
game: Game.Game;
playHandsign: Function;
playHandsign: (handsign: Msg.Handsign) => Promise<void>;
leaveGame: Function;
claimInactivity: () => Promise<void>;
enqueueSnackbar: Function;
Expand All @@ -61,16 +61,19 @@ const GamePlaying = (props: Props) => {
const { game, playHandsign, leaveGame, claimInactivity, enqueueSnackbar } = props;
const [pickedRound, setPickedRound] = useState<number>();
const [claimingInactivity, setClaimingInactivity] = useState<boolean>(false);
const pickHandsign = (handsign: Msg.Handsign) => {
const pickHandsign = async (handsign: Msg.Handsign) => {
setPickedRound(game.round);
playHandsign(handsign);
try {
await playHandsign(handsign);
} catch {
setPickedRound(undefined);
}
};
const tryClaimInactivity = async () => {
setClaimingInactivity(true);
try {
await claimInactivity();
} catch {
enqueueSnackbar('Secret error', { variant: 'error' });
setClaimingInactivity(false);
}
};
Expand Down
10 changes: 8 additions & 2 deletions web/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,21 @@ const playHandsign = async (
try {
await client.execute(game.contract, { play_hand: { handsign, locator: game.locator } });
} catch (e) {
if (e.message !== 'ciphertext not set') throw e;
if (e instanceof Error) {
if (e.message === 'ciphertext not set') return;
}
throw e;
}
};

const claimInactivity = async (client: SecretJS.SigningCosmWasmClient, game: Game) => {
try {
await client.execute(game.contract, { claim_inactivity: { locator: game.locator } });
} catch (e) {
if (e.message !== 'ciphertext not set') throw e;
if (e instanceof Error) {
if (e.message === 'ciphertext not set') return;
}
throw e;
}
};

Expand Down

0 comments on commit a63012c

Please sign in to comment.