Skip to content

Commit

Permalink
feat: throw an exception if provided call method does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
fracek committed Aug 11, 2023
1 parent fc5bbad commit 5836c47
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-numbers-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@starknet-react/core': patch
---

Throw exception if a contract method doesn't exist
26 changes: 26 additions & 0 deletions packages/core/src/hooks/call.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,30 @@ describe('useStarknetCall', () => {
})
})
})

describe('when function does not exist', () => {
it('returns an error', async () => {
const { result } = renderHook(() =>
useTestHook({ functionName: 'this_one_doesnt_exist', address, args: [] })
)

await waitFor(() => {
expect(result.current.error).toBeDefined()
expect(result.current.status).toEqual('error')
expect(result.current.data).toBeUndefined()
expect(result.current.isIdle).toBeTruthy()
expect(result.current.isLoading).toBeFalsy()
expect(result.current.isFetching).toBeFalsy()
expect(result.current.isSuccess).toBeFalsy()
expect(result.current.isError).toBeTruthy()
expect(result.current.isFetched).toBeTruthy()
expect(result.current.isFetchedAfterMount).toBeTruthy()
expect(result.current.isRefetching).toBeFalsy()
},
{
timeout: 30000,
interval: 1000,
})
})
})
})
6 changes: 4 additions & 2 deletions packages/core/src/hooks/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,10 @@ interface ReadContractArgs {
function readContract({ args }: { args: ReadContractArgs }) {
return async () => {
if (!args.args || !args.contract || !args.functionName) return null
const call = args.contract && args.functionName && args.contract[args.functionName]
if (!call) return null
const canCall = args.contract && args.functionName
if (!canCall) return null
const call = args.contract[args.functionName]
if (!call) throw new Error(`Function ${args.functionName} not found on contract`)

return await call(...args.args, {
blockIdentifier: args.blockIdentifier,
Expand Down

0 comments on commit 5836c47

Please sign in to comment.