Skip to content

Commit 07b29de

Browse files
authored
400 for user errors (#751)
1 parent 8539938 commit 07b29de

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/server/routes/contract/read/read.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Type } from "@sinclair/typebox";
22
import type { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
44
import { getContract } from "../../../../utils/cache/getContract";
5+
import { createCustomError } from "../../../middleware/error";
56
import {
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
});

test/e2e/tests/read.test.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { beforeAll, describe, test } from "bun:test";
1+
import { beforeAll, describe, expect, test } from "bun:test";
22
import { sepolia } from "thirdweb/chains";
3-
import { expect } from "vitest";
3+
import type { ApiError } from "../../../sdk/dist/thirdweb-dev-engine.cjs";
44
import type { setupEngine } from "../utils/engine";
55
import { 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
});

0 commit comments

Comments
 (0)