Skip to content

Commit

Permalink
fix: fix snapshot hooks error messages (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
1M4nt0 authored Oct 17, 2023
1 parent 6541184 commit 9250579
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/snapshot/src/hooks/actions/useCreateProposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import snapshot from '@snapshot-labs/snapshot.js'
import { useEthersSigner } from './useEthersSigner'
import { ChainSnapshotHub } from '@dae/chains'
import { Proposal } from '@snapshot-labs/snapshot.js/dist/sign/types'
import { useHookState } from './useHookState'
import { useSnapshotHookState } from './useSnapshotHookState'

type ProposalCreationResult = {
id: string
Expand All @@ -20,7 +20,7 @@ export const useCreateProposal = (snapshotSpaceENS: string | undefined) => {
const { data: blockNumber } = useBlockNumber()

const { isSuccess, isValidating, isLoading, isError, error, ...state } =
useHookState()
useSnapshotHookState()

const spaceNetwork = chain!.id as keyof typeof ChainSnapshotHub
const hub = ChainSnapshotHub[spaceNetwork]
Expand Down
4 changes: 2 additions & 2 deletions packages/snapshot/src/hooks/actions/useCreateSnapshotSpace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import snapshot from '@snapshot-labs/snapshot.js'
import { useEthersSigner } from './useEthersSigner'
import { ChainSnapshotHub } from '@dae/chains'
import { type VotingStrategy } from '@dae/types'
import { useHookState } from './useHookState'
import { useSnapshotHookState } from './useSnapshotHookState'
import { SpaceConfig } from '../../types'
import { mutate } from 'swr'

Expand Down Expand Up @@ -80,7 +80,7 @@ export const useCreateSnapshotSpace = () => {
const { address } = useAccount()
const { chain } = useNetwork()
const { isSuccess, isValidating, isLoading, isError, error, ...state } =
useHookState()
useSnapshotHookState()

const spaceNetwork = chain!.id as keyof typeof ChainSnapshotHub
const hub = ChainSnapshotHub[spaceNetwork]
Expand Down
4 changes: 2 additions & 2 deletions packages/snapshot/src/hooks/actions/useEditSnapshotSpace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Address, useAccount } from 'wagmi'
import snapshot from '@snapshot-labs/snapshot.js'
import { useEthersSigner } from './useEthersSigner'
import { ChainSnapshotHub } from '@dae/chains'
import { useHookState } from './useHookState'
import { useSnapshotHookState } from './useSnapshotHookState'
import { useCourseSpace } from '../api'
import { mutate } from 'swr'

Expand All @@ -11,7 +11,7 @@ export const useEditSnapshotSpace = (
chainId: number | undefined,
) => {
const { isSuccess, isValidating, isLoading, isError, error, ...state } =
useHookState()
useSnapshotHookState()
const { address } = useAccount()
const spaceNetwork = chainId as keyof typeof ChainSnapshotHub
const hub = ChainSnapshotHub[spaceNetwork]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type HookState = {
isValidating: boolean
}

export function useHookState() {
export function useSnapshotHookState() {
const [state, setState] = useState<HookState>({
error: null,
isError: false,
Expand Down Expand Up @@ -39,13 +39,22 @@ export function useHookState() {

const handleError = (error: unknown) => {
let parsedError: Error
switch (true) {
case error instanceof Error:
parsedError = error as Error
break
default:
parsedError = new Error('An error occurred')

if (
error &&
typeof error === 'object' &&
'error' in error &&
'error_description' in error
) {
parsedError = new Error(
`Snapshot service returned an error: ${
error.error_description as string
}.`,
)
} else {
parsedError = new Error('An error occurred.')
}

setState({
...state,
isSuccess: false,
Expand Down
4 changes: 2 additions & 2 deletions packages/snapshot/src/hooks/actions/useVoteProposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import snapshot from '@snapshot-labs/snapshot.js'
import { useAccount } from 'wagmi'
import { ChainSnapshotHub } from '@dae/chains'
import { ProposalType } from '@snapshot-labs/snapshot.js/dist/sign/types'
import { useHookState } from './useHookState'
import { useSnapshotHookState } from './useSnapshotHookState'
import { mutate } from 'swr'

export const useVotePropsal = (
Expand All @@ -14,7 +14,7 @@ export const useVotePropsal = (
) => {
const { address } = useAccount()
const { isSuccess, isValidating, isLoading, isError, error, ...state } =
useHookState()
useSnapshotHookState()

const hub = ChainSnapshotHub[spaceNetwork]
const snapshotClient = new snapshot.Client712(hub)
Expand Down

0 comments on commit 9250579

Please sign in to comment.