Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

Fix Zod Error Handling on V0 (#2488)

Abimael Martell883cd913109c
diff --git a/apps/api/src/__tests__/snips/v0/lib.ts b/apps/api/src/__tests__/snips/v0/lib.ts
new file mode 100644
index 000000000..a669ae518
--- /dev/null
+++ b/apps/api/src/__tests__/snips/v0/lib.ts
@@ -0,0 +1,32 @@
+import request from "supertest";
+import {
+ TEST_API_URL,
+ scrapeTimeout,
+ indexCooldown,
+ Identity,
+ idmux,
+} from "../lib";
+
+// Re-export shared utilities for backwards compatibility
+export { scrapeTimeout, indexCooldown, Identity, idmux };
+
+export interface V0ScrapeRequestInput {
+ url: string;
+ pageOptions?: any;
+ extractorOptions?: any;
+ crawlerOptions?: any;
+ timeout?: number;
+ origin?: string;
+ integration?: string;
+}
+
+export async function scrapeRaw(
+ body: V0ScrapeRequestInput,
+ identity: Identity,
+) {
+ return await request(TEST_API_URL)
+ .post("/v0/scrape")
+ .set("Authorization", `Bearer ${identity.apiKey}`)
+ .set("Content-Type", "application/json")
+ .send(body);
+}
diff --git a/apps/api/src/__tests__/snips/v0/scrape.test.ts b/apps/api/src/__tests__/snips/v0/scrape.test.ts
new file mode 100644
index 000000000..29fed1e39
--- /dev/null
+++ b/apps/api/src/__tests__/snips/v0/scrape.test.ts
@@ -0,0 +1,122 @@
+import {
+ concurrentIf,
+ ALLOW_TEST_SUITE_WEBSITE,
+ TEST_SUITE_WEBSITE,
+} from "../lib";
+import { scrapeRaw, idmux, Identity } from "./lib";
+
+let identity: Identity;
+
+beforeAll(async () => {
+ identity = await idmux({
+ name: "v0-scrape",
+ concurrency: 100,
+ credits: 1000000,
+ });
+}, 10000);
+
+describe("V0 Scrape tests", () => {
+ describe("URL validation", () => {
+ it.concurrent("rejects invalid URL format", async () => {
+ const response = await scrapeRaw(
+ {
+ url: "not-a-valid-url",
+ },
+ identity,
+ );
+
+ expect(response.statusCode).toBe(400);
+ expect(response.body.success).toBe(false);
+ expect(response.body.error).toBeDefined();
+ expect(typeof response.body.error).toBe("string");
+ // Should contain validation error messages
+ expect(
+ response.body.error.includes("Invalid URL") ||
+ response.body.error.includes("valid top-level domain"),
+ ).toBe(true);
+ });
+
+ it.concurrent("rejects URL without protocol", async () => {
+ const response = await scrapeRaw(
+ {
+ url: "example.com",
+ },
+ identity,
+ );
+
+ // Note: The schema adds http:// prefix, so this might pass validation
+ // But if it fails, it should return 400
+ if (response.statusCode === 400) {
+ expect(response.body.success).toBe(false);
+ expect(response.body.error).toBeDefined();
+ }
+ });
+
+ it.concurrent("rejects URL with unsupported protocol", async () => {
+ const response = await scrapeRaw(
+ {
+ url: "ftp://example.com",
+ },
+ identity,
+ );
+
+ expect(response.statusCode).toBe(400);
+ expect(response.body.success).toBe(false);
+ expect(response.body.error).toBeDefined();
+ expect(response.body.error).toContain("unsupported protocol");
+ });
+
+ it.concurrent("rejects empty URL", async () => {
+ const response = await scrapeRaw(
+ {
+ url: "",
+ },
+ identity,
+ );
+
+ expect(response.statusCode).toBe(400);
+ expect(response.body.success).toBe(false);
+ expect(response.body.error).toBeDefined();
+ });
+
+ it.concurrent("rejects URL without top-level domain", async () => {
+ const response = await scrapeRaw(
+ {
+ url: "http://localhost",
+ },
+ identity,
+ );
+
+ // This might pass in self-hosted mode, but should fail in production
+ if (response.statusCode === 400) {
+ expect(response.body.success).toBe(false);
+ expect(response.body.error).toBeDefined();
+ expect(
+ response.body.error.includes("valid top-level domain") ||
+ response.body.error.includes("Invalid URL"),
+ ).toBe(true);
+ }
+ });
+
+ concurrentIf(ALLOW_TEST_SUITE_WEBSITE)(
+ "accepts valid URL without validation errors",
+ async () => {
+ const response = await scrapeRaw(
+ {
+ url: TEST_SUITE_WEBSITE,
+ },
+ identity,
+ );
+
+ // Valid URL should not return 400 for URL validation errors
+ // If it returns 400, the error should not be about URL validation
+ if (response.statusCode === 400) {
+ expect(response.body.error).not.toContain("Invalid URL");
+ expect(response.body.error).not.toContain("valid top-level domain");
+ expect(response.body.error).not.toContain("unsupported protocol");
+ }
+ },
+ 60000,
+ );
+ });
+});
diff --git a/apps/api/src/controllers/v0/scrape.ts b/apps/api/src/controllers/v0/scrape.ts
index 4a9e91d41..337c795e7 100644
--- a/apps/api/src/controllers/v0/scrape.ts
+++ b/apps/api/src/controllers/v0/scrape.ts
@@ -2,7 +2,7 @@ import { ExtractorOptions, PageOptions } from "./../../lib/entities";
import { Request, Response } from "express";
import { checkTeamCredits } from "../../services/billing/credit_billing";
import { authenticateUser } from "../auth";
-import { RateLimiterMode } from "../../types";
+import { RateLimiterMode, AuthResponse } from "../../types";
import { TeamFlags, toLegacyDocument, url as urlSchema } from "../v1/types";
import { isUrlBlocked } from "../../scraper/WebScraper/utils/blocklist"; // Import the isUrlBlocked function
import {
@@ -42,7 +42,21 @@ async function scrapeHelper(
data?: V0Document | { url: string };
returnCode: number;
}> {
- const url = urlSchema.parse(req.body.url);
+ let url: string;
+ try {
+ url = urlSchema.parse(req.body.url);
+ } catch (error) {
+ if (error instanceof ZodError) {
+ const errorMessage =
+ error.issues
+ .map(issue => issue.message)
+ .filter((msg, idx, arr) => arr.indexOf(msg) === idx)
+ .join("; ") || "Invalid URL";
+
+ return { success: false, error: errorMessage, returnCode: 400 };
+ }
+ throw error;
+ }
if (typeof url !== "string") {
return { success: false, error: "Url is required", returnCode: 400 };
}