Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/__tests__/commands/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,20 @@ describe('executeSearch', () => {
});
});

it("shows the API's own error for a refused request, not the HTTP status", async () => {
const apiError =
'Insufficient credits to perform this request. For more credits, you can upgrade your plan at https://firecrawl.dev/pricing or try changing the request limit to a lower value.';
mockHttpPost.mockRejectedValue(
Object.assign(new Error('Request failed with status code 402'), {
response: { status: 402, data: { success: false, error: apiError } },
})
);

const result = await executeSearch({ query: 'test query' });

expect(result).toEqual({ success: false, error: apiError });
});

it('should handle non-Error exceptions', async () => {
mockHttpPost.mockRejectedValue('String error');

Expand Down
12 changes: 9 additions & 3 deletions src/commands/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,19 @@ export async function executeSearch(
error:
options.domainTools || options.sources?.includes('alexandria')
? JSON.stringify(apiFailure(error))
: error instanceof Error
? error.message
: 'Unknown error occurred',
: apiErrorMessage(error),
};
}
}

/** The API's own error text, which a raw HTTP error keeps only in its response body. */
function apiErrorMessage(error: unknown): string {
const body = (error as { response?: { data?: { error?: unknown } } })
?.response?.data;
if (typeof body?.error === 'string' && body.error) return body.error;
return error instanceof Error ? error.message : 'Unknown error occurred';
}

/**
* Shorten a matched passage for the human-readable output. A developer passage
* runs to several KB, which floods a terminal. `--json` keeps the full text.
Expand Down
Loading