Repository navigation
fix(crawl): surface API errors and failed pages from crawl status #462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RaphaelFakhri
wants to merge
1
commit into
firecrawl:main
Choose a base branch
from
RaphaelFakhri:fix/crawl-status-api-errors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { spawn } from 'node:child_process'; | ||
| import { createServer } from 'node:http'; | ||
| import test from 'node:test'; | ||
| import { setTimeout as delay } from 'node:timers/promises'; | ||
|
|
||
| const JOB = '00000000-0000-4000-8000-000000000050'; | ||
| const MISSING = '00000000-0000-4000-8000-000000000051'; | ||
| const BROKEN_PAGE = '00000000-0000-4000-8000-000000000052'; | ||
| const FAILED_START = '00000000-0000-4000-8000-000000000053'; | ||
|
|
||
| async function startFakeApi() { | ||
| const server = createServer((req, res) => { | ||
| const json = (status, body) => { | ||
| res.writeHead(status, { 'content-type': 'application/json' }); | ||
| res.end(JSON.stringify(body)); | ||
| }; | ||
| const origin = `http://${req.headers.host}`; | ||
| if (req.url === `/v2/crawl/${MISSING}`) { | ||
| return json(404, { success: false, error: 'Job not found' }); | ||
| } | ||
| if (req.url === `/v2/crawl/${FAILED_START}`) { | ||
| return json(200, { | ||
| success: false, | ||
| error: 'Crawl unavailable', | ||
| }); | ||
| } | ||
| if (req.url === `/v2/crawl/${BROKEN_PAGE}`) { | ||
| return json(200, { | ||
| success: true, | ||
| status: 'completed', | ||
| completed: 2, | ||
| total: 2, | ||
| data: [{ markdown: 'one' }], | ||
| next: `${origin}/v2/crawl/${BROKEN_PAGE}?skip=1`, | ||
| }); | ||
| } | ||
| if (req.url === `/v2/crawl/${BROKEN_PAGE}?skip=1`) { | ||
| return json(200, { | ||
| success: false, | ||
| error: 'Page expired', | ||
| }); | ||
| } | ||
| if (req.url === `/v2/crawl/${JOB}`) { | ||
| return json(200, { | ||
| success: true, | ||
| status: 'completed', | ||
| completed: 2, | ||
| total: 2, | ||
| data: [{ markdown: 'one' }], | ||
| next: `${origin}/v2/crawl/${JOB}?skip=1`, | ||
| }); | ||
| } | ||
| if (req.url === `/v2/crawl/${JOB}?skip=1`) { | ||
| return json(200, { success: true, data: [{ markdown: 'two' }] }); | ||
| } | ||
| json(404, { error: `Unhandled ${req.method} ${req.url}` }); | ||
| }); | ||
| await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); | ||
| return { | ||
| url: `http://127.0.0.1:${server.address().port}`, | ||
| close: () => new Promise((resolve) => server.close(resolve)), | ||
| }; | ||
| } | ||
|
|
||
| async function startMcp(t, apiUrl) { | ||
| const child = spawn(process.execPath, ['dist/index.js'], { | ||
| env: { ...process.env, FIRECRAWL_API_KEY: 'fc-test', FIRECRAWL_API_URL: apiUrl }, | ||
| stdio: ['pipe', 'pipe', 'pipe'], | ||
| }); | ||
| t.after(async () => { | ||
| child.kill('SIGTERM'); | ||
| await Promise.race([new Promise((r) => child.once('exit', r)), delay(2_000)]); | ||
| }); | ||
| child.stdout.setEncoding('utf8'); | ||
| let buffer = ''; | ||
| const pending = new Map(); | ||
| let nextId = 0; | ||
| child.once('exit', (code, signal) => { | ||
| const error = new Error(`MCP server exited: code=${code} signal=${signal}`); | ||
| for (const { reject } of pending.values()) reject(error); | ||
| pending.clear(); | ||
| }); | ||
| child.stdout.on('data', (chunk) => { | ||
| buffer += chunk; | ||
| let newline; | ||
| while ((newline = buffer.indexOf('\n')) !== -1) { | ||
| const line = buffer.slice(0, newline).trim(); | ||
| buffer = buffer.slice(newline + 1); | ||
| if (!line) continue; | ||
| const message = JSON.parse(line); | ||
| const waiter = pending.get(message.id); | ||
| if (!waiter) continue; | ||
| pending.delete(message.id); | ||
| if (message.error) waiter.reject(new Error(message.error.message)); | ||
| else waiter.resolve(message.result); | ||
| } | ||
| }); | ||
| const request = (method, params = {}) => { | ||
| const id = ++nextId; | ||
| child.stdin.write(`${JSON.stringify({ id, jsonrpc: '2.0', method, params })}\n`); | ||
| return new Promise((resolve, reject) => { | ||
| const timeout = setTimeout(() => { | ||
| pending.delete(id); | ||
| reject(new Error(`timeout: ${method}`)); | ||
| }, 10_000); | ||
| pending.set(id, { | ||
| reject: (error) => { | ||
| clearTimeout(timeout); | ||
| reject(error); | ||
| }, | ||
| resolve: (value) => { | ||
| clearTimeout(timeout); | ||
| resolve(value); | ||
| }, | ||
| }); | ||
| }); | ||
| }; | ||
| await request('initialize', { | ||
| capabilities: {}, | ||
| clientInfo: { name: 'crawl-status-test', version: '0.0.0' }, | ||
| protocolVersion: '2025-06-18', | ||
| }); | ||
| child.stdin.write(`${JSON.stringify({ jsonrpc: '2.0', method: 'notifications/initialized' })}\n`); | ||
| return (id) => | ||
| request('tools/call', { name: 'firecrawl_check_crawl_status', arguments: { id } }); | ||
| } | ||
|
|
||
| test('firecrawl_check_crawl_status joins every page of a paginated crawl', async (t) => { | ||
| const api = await startFakeApi(); | ||
| t.after(() => api.close()); | ||
| const check = await startMcp(t, api.url); | ||
|
|
||
| const result = await check(JOB); | ||
| assert.notEqual(result.isError, true); | ||
| assert.deepEqual( | ||
| result.structuredContent.data.map((doc) => doc.markdown), | ||
| ['one', 'two'] | ||
| ); | ||
| }); | ||
|
|
||
| test('firecrawl_check_crawl_status reports the API error for an unknown crawl', async (t) => { | ||
| const api = await startFakeApi(); | ||
| t.after(() => api.close()); | ||
| const check = await startMcp(t, api.url); | ||
|
|
||
| const result = await check(MISSING); | ||
| assert.equal(result.isError, true); | ||
| assert.match(result.content[0].text, /Job not found/); | ||
| }); | ||
|
|
||
| test('firecrawl_check_crawl_status fails when a result page cannot be read', async (t) => { | ||
| const api = await startFakeApi(); | ||
| t.after(() => api.close()); | ||
| const check = await startMcp(t, api.url); | ||
|
|
||
| const result = await check(BROKEN_PAGE); | ||
| assert.equal(result.isError, true); | ||
| assert.match(result.content[0].text, /Page expired/); | ||
| }); | ||
|
|
||
| test('firecrawl_check_crawl_status reports a failure in the first response', async (t) => { | ||
| const api = await startFakeApi(); | ||
| t.after(() => api.close()); | ||
| const check = await startMcp(t, api.url); | ||
|
|
||
| const result = await check(FAILED_START); | ||
| assert.equal(result.isError, true); | ||
| assert.match(result.content[0].text, /Crawl unavailable/); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.