Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(web-crawler): make crawl failures traceable per impl (#16848)

YuTengjingf0353c46ce11
diff --git a/packages/web-crawler/src/crawImpl/__tests__/jina.test.ts b/packages/web-crawler/src/crawImpl/__tests__/jina.test.ts
index ada54da305e..8aa3a39cee6 100644
--- a/packages/web-crawler/src/crawImpl/__tests__/jina.test.ts
+++ b/packages/web-crawler/src/crawImpl/__tests__/jina.test.ts
@@ -186,17 +186,17 @@ describe('jina crawler', () => {
});
});
- it('should return undefined if response is not ok', async () => {
+ it('should throw HTTP status error if response is not ok', async () => {
mockFetch.mockResolvedValue(
- createMockResponse(null, { ok: false, status: 500, statusText: 'Internal Server Error' }),
+ createMockResponse(null, { ok: false, status: 429, statusText: 'Too Many Requests' }),
);
- const result = await jina('https://example.com', { filterOptions: {} });
-
- expect(result).toBeUndefined();
+ await expect(jina('https://example.com', { filterOptions: {} })).rejects.toThrow(
+ 'Jina request failed with status 429: Too Many Requests',
+ );
});
- it('should return undefined if response code is not 200', async () => {
+ it('should throw error if response code is not 200', async () => {
const mockResponse = createMockResponse(
{
code: 400,
@@ -207,9 +207,9 @@ describe('jina crawler', () => {
mockFetch.mockResolvedValue(mockResponse);
- const result = await jina('https://example.com', { filterOptions: {} });
-
- expect(result).toBeUndefined();
+ await expect(jina('https://example.com', { filterOptions: {} })).rejects.toThrow(
+ 'Jina request failed with code 400: Bad Request',
+ );
});
it('should throw error if fetch throws non-fetch-failed error', async () => {
diff --git a/packages/web-crawler/src/crawImpl/jina.ts b/packages/web-crawler/src/crawImpl/jina.ts
index a948b111a11..52d4526427d 100644
--- a/packages/web-crawler/src/crawImpl/jina.ts
+++ b/packages/web-crawler/src/crawImpl/jina.ts
@@ -2,7 +2,7 @@ import { getJinaReaderBaseUrl } from '@lobechat/utils';
import type { CrawlImpl } from '../type';
import { toFetchError } from '../utils/errorType';
-import { parseJSONResponse } from '../utils/response';
+import { createHTTPStatusError, parseJSONResponse } from '../utils/response';
import { withTimeout } from '../utils/withTimeout';
const JINA_TIMEOUT = 15_000;
@@ -29,7 +29,7 @@ export const jina: CrawlImpl<{ apiKey?: string }> = async (url, params) => {
}
if (!res.ok) {
- return;
+ throw await createHTTPStatusError(res, 'Jina');
}
const json = await parseJSONResponse<{
@@ -40,10 +40,15 @@ export const jina: CrawlImpl<{ apiKey?: string }> = async (url, params) => {
siteName?: string;
title?: string;
};
+ message?: string;
}>(res, 'Jina');
if (json.code !== 200) {
- return;
+ throw new Error(
+ json.message
+ ? `Jina request failed with code ${json.code}: ${json.message}`
+ : `Jina request failed with code ${json.code}`,
+ );
}
const result = json.data;
diff --git a/packages/web-crawler/src/crawImpl/naive.ts b/packages/web-crawler/src/crawImpl/naive.ts
index a28e66f417b..57cf7be87cd 100755
--- a/packages/web-crawler/src/crawImpl/naive.ts
+++ b/packages/web-crawler/src/crawImpl/naive.ts
@@ -109,7 +109,7 @@ export const naive: CrawlImpl = async (url, { filterOptions }) => {
url,
} satisfies CrawlSuccessResult;
} catch (error) {
- console.error(error);
+ console.error('[naive]', error);
}
return;
diff --git a/packages/web-crawler/src/crawler.ts b/packages/web-crawler/src/crawler.ts
index 7d04dec29ca..dcad7d3b3bb 100644
--- a/packages/web-crawler/src/crawler.ts
+++ b/packages/web-crawler/src/crawler.ts
@@ -51,7 +51,9 @@ export class Crawler {
const filteredRuleImpls = ruleImpls
? (ruleImpls.filter((impl) => this.impls.includes(impl as CrawlImplType)) as CrawlImplType[])
: undefined;
- const systemImpls = (filteredRuleImpls?.length ? filteredRuleImpls : this.impls) as CrawlImplType[];
+ const systemImpls = (
+ filteredRuleImpls?.length ? filteredRuleImpls : this.impls
+ ) as CrawlImplType[];
const finalImpls = userImpls
? (userImpls.filter((impl) => Object.keys(crawlImpls).includes(impl)) as CrawlImplType[])
@@ -71,11 +73,14 @@ export class Crawler {
};
}
+ console.error(
+ `[${impl}] returned empty or short content (length: ${res?.content?.length ?? 0})`,
+ );
finalError = new Error(`${impl} returned empty or short content`);
finalError.name = 'EmptyCrawlResultError';
finalCrawler = impl;
} catch (error) {
- console.error(error);
+ console.error(`[${impl}]`, error);
finalError = error as Error;
finalCrawler = impl;
}