File tree Expand file tree Collapse file tree 2 files changed +43
-4
lines changed
src/server/routes/contract/read Expand file tree Collapse file tree 2 files changed +43
-4
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import { Type } from "@sinclair/typebox";
22import type { FastifyInstance } from "fastify" ;
33import { StatusCodes } from "http-status-codes" ;
44import { getContract } from "../../../../utils/cache/getContract" ;
5+ import { createCustomError } from "../../../middleware/error" ;
56import {
67 readRequestQuerySchema ,
78 type readSchema ,
@@ -62,11 +63,28 @@ export async function readContract(fastify: FastifyInstance) {
6263 return arg ;
6364 } ) ;
6465
65- let returnData = await contract . call ( functionName , parsedArgs ?? [ ] ) ;
66+ let returnData : unknown ;
67+
68+ try {
69+ returnData = await contract . call ( functionName , parsedArgs ?? [ ] ) ;
70+ } catch ( e ) {
71+ if (
72+ e instanceof Error &&
73+ ( e . message . includes ( "is not a function" ) ||
74+ e . message . includes ( "arguments, but" ) )
75+ ) {
76+ throw createCustomError (
77+ e . message ,
78+ StatusCodes . BAD_REQUEST ,
79+ "BAD_REQUEST" ,
80+ ) ;
81+ }
82+ }
6683 returnData = bigNumberReplacer ( returnData ) ;
6784
6885 reply . status ( StatusCodes . OK ) . send ( {
69- result : returnData ,
86+ // biome-ignore lint/suspicious/noExplicitAny: data from chain
87+ result : returnData as any ,
7088 } ) ;
7189 } ,
7290 } ) ;
Original file line number Diff line number Diff line change 1- import { beforeAll , describe , test } from "bun:test" ;
1+ import { beforeAll , describe , expect , test } from "bun:test" ;
22import { sepolia } from "thirdweb/chains" ;
3- import { expect } from "vitest " ;
3+ import type { ApiError } from "../../../sdk/dist/thirdweb-dev-engine.cjs " ;
44import type { setupEngine } from "../utils/engine" ;
55import { setup } from "./setup" ;
66
@@ -35,4 +35,25 @@ describe("Read Tests", () => {
3535 expect ( result [ 2 ] ) . toEqual ( "1" ) ;
3636 expect ( result [ 3 ] ) . toEqual ( "2" ) ;
3737 } ) ;
38+
39+ test ( "Incorrectly read a contract should 400 (incorrect arity)" , async ( ) => {
40+ const structValues = {
41+ name : "test" ,
42+ value : 123 ,
43+ } ;
44+
45+ const structString = JSON . stringify ( structValues ) ;
46+
47+ try {
48+ await engine . contract . read (
49+ "readStructAndInts" ,
50+ chainIdString ,
51+ structContractAddress ,
52+ [ structString , 1 ] . join ( "," ) ,
53+ ) ;
54+ throw new Error ( "Expected method to throw" ) ;
55+ } catch ( error ) {
56+ expect ( ( error as ApiError ) . status ) . toBe ( 400 ) ;
57+ }
58+ } ) ;
3859} ) ;
You can’t perform that action at this time.
0 commit comments