Skip to content

Commit

Permalink
Merge pull request #15 from subspace/ft/improveErrorHandling
Browse files Browse the repository at this point in the history
Improve error handling
  • Loading branch information
marc-aurele-besner authored Nov 7, 2023
2 parents 3ef071f + b170714 commit 3accaa5
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 9 deletions.
9 changes: 5 additions & 4 deletions discord-bot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
faucetBalanceLowSlackMessage,
findRequest,
findStats,
formatSeconds,
log,
queries,
requestTokens,
Expand Down Expand Up @@ -200,14 +201,14 @@ export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayPr
}
} else {
const timeToWait = nextAccessTime.sub(currentTime)
const formattedTime = timeToWait.toString()
const formattedTime = timeToWait.toNumber()
log('nextAccessTime', formattedTime)
await postDiscordMessage(
rest,
body.channel_id,
`:clock1: ${tagUser(
body.member.user.id,
)} Please wait ${formattedTime} seconds before requesting again`,
`:clock1: ${tagUser(body.member.user.id)} Please wait ${formatSeconds(
formattedTime,
)} before requesting again`,
)
return finishInteraction()
}
Expand Down
1 change: 1 addition & 0 deletions discord-bot/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './config'
export * from './contracts'
export * from './fauna'
export * from './slack'
export * from './time'
6 changes: 6 additions & 0 deletions discord-bot/src/utils/time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const formatSeconds = (timeInSeconds: number): string => {
const hours = Math.floor(((timeInSeconds % 31536000) % 86400) / 3600)
const minutes = Math.floor((((timeInSeconds % 31536000) % 86400) % 3600) / 60)
const seconds = (((timeInSeconds % 31536000) % 86400) % 3600) % 60
return hours + ' hours ' + minutes + ' minutes ' + seconds + ' seconds'
}
5 changes: 5 additions & 0 deletions web-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,10 @@
"prettier": "^3.0.0",
"typescript": "^5.2.2",
"url-loader": "^4.1.1"
},
"browser": {
"fs": false,
"path": false,
"os": false
}
}
4 changes: 0 additions & 4 deletions web-app/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export const authOptions = (): AuthOptions => {
}
}),
DiscordProvider({
// authorization: { params: { scope: 'identify guilds' } },
clientId: process.env.DISCORD_CLIENT_ID || '',
clientSecret: process.env.DISCORD_CLIENT_SECRET || '',
authorization: { params: { scope: 'identify guilds guilds.members.read' } },
Expand All @@ -52,9 +51,6 @@ export const authOptions = (): AuthOptions => {
headers: {
Authorization: `Bearer ${token.access_token}`
}
// body: JSON.stringify({
// target_user_id: '1382564789444964354'
// })
})
.then((res) => res.json())
.then((json) => {
Expand Down
22 changes: 21 additions & 1 deletion web-app/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { formatUnits } from 'viem'
import { useAccount, useContractReads, useNetwork } from 'wagmi'
import { Contract, contracts } from '../constants/contracts'
import { nova } from '../constants/networks'
import { formatSeconds } from '../utils'

interface CheckedOrNotProps {
isChecked: boolean
Expand Down Expand Up @@ -107,6 +108,23 @@ const RequestTokenButton: React.FC<RequestTokenButtonProps> = ({ contract, addre

const handleRequestToken = async () => {
if (chain && !dataIsError && data && data[0] && data[1] && session != null && session.user != null) {
if (session.user.accountType === 'github' && !session.user.isGitHubFollower)
return toast({
title: 'Error requesting token',
description:
'Please make sure you are following Subspace GitHub account and connect your GitHub account again.',
status: 'error',
duration: 9000,
isClosable: true
})
if (session.user.accountType === 'discord' && !session.user.isDiscordGuildMember)
return toast({
title: 'Error requesting token',
description: 'Please make sure you are member of the Discord server and connect your Discord account again.',
status: 'error',
duration: 9000,
isClosable: true
})
setIsLoading(true)
const withdrawalAmount = data[0].status === 'success' ? (data[0].result as bigint) : BigInt(0)
const nextAccessTime = data[1].status === 'success' ? (data[1].result as bigint) : BigInt(0)
Expand All @@ -115,7 +133,9 @@ const RequestTokenButton: React.FC<RequestTokenButtonProps> = ({ contract, addre
case now < nextAccessTime:
toast({
title: 'Error requesting token',
description: `You can request token again in ${(BigInt(nextAccessTime) - BigInt(now)).toString()} seconds.`,
description: `You can request token again in ${formatSeconds(
Number(BigInt(nextAccessTime) - BigInt(now))
)}.`,
status: 'error',
duration: 9000,
isClosable: true
Expand Down
1 change: 1 addition & 0 deletions web-app/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './fauna'
export * from './slack'
export * from './time'
6 changes: 6 additions & 0 deletions web-app/src/utils/time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const formatSeconds = (timeInSeconds: number): string => {
const hours = Math.floor(((timeInSeconds % 31536000) % 86400) / 3600)
const minutes = Math.floor((((timeInSeconds % 31536000) % 86400) % 3600) / 60)
const seconds = (((timeInSeconds % 31536000) % 86400) % 3600) % 60
return hours + ' hours ' + minutes + ' minutes ' + seconds + ' seconds'
}

0 comments on commit 3accaa5

Please sign in to comment.