Skip to content

Commit

Permalink
fix: Handle 422 error for infobox entity type mismatch
Browse files Browse the repository at this point in the history
- Add retry mechanism for 422 errors related to infobox results
- Update BraveSearchError to include response data
- Modify webSearch method to retry with all result types in filter
- Update error handling to provide more detailed information
  • Loading branch information
erik-balfe committed Jul 8, 2024
1 parent d1cc3a6 commit 3bc6492
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "brave-search",
"version": "0.5.4",
"version": "0.5.5",
"description": "A TypeScript library for interacting with the Brave Search API, including web search, local POI search, and automatic summarization features.",
"scripts": {
"build": "tsc",
Expand Down
61 changes: 55 additions & 6 deletions src/braveSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import axios from "axios";

const DEFAULT_POLLING_TIMEOUT = 20000;

import {
BraveSearchOptions,
LocalDescriptionsOptions,
Expand All @@ -27,17 +29,20 @@ import {
} from "./types";

class BraveSearchError extends Error {
constructor(message: string) {
public responseData: any;

constructor(message: string, responseData?: any) {
super(message);
this.name = "BraveSearchError";
this.responseData = responseData;
}
}

class BraveSearch {
private apiKey: string;
private baseUrl = "https://api.search.brave.com/res/v1";
private maxPollAttempts = 5;
private pollInterval = 500;
private maxPollAttempts = DEFAULT_POLLING_TIMEOUT / this.pollInterval;

constructor(
apiKey: string,
Expand Down Expand Up @@ -68,7 +73,32 @@ class BraveSearch {
);
return response.data;
} catch (error) {
throw this.handleApiError(error);
const handledError = this.handleApiError(error);
if (
handledError.message.includes(
"Retrying with modified 'result_filter' option in request",
)
) {
// Retry the request with a modified result_filter
const modifiedParams = new URLSearchParams(params);
modifiedParams.set(
"result_filter",
"discussions,faq,news,query,summarizer,videos,web,infobox",
);

try {
const response = await axios.get<WebSearchApiResponse>(
`${this.baseUrl}/web/search?${modifiedParams.toString()}`,
{
headers: this.getHeaders(),
},
);
return response.data;
} catch (retryError) {
throw this.handleApiError(retryError);
}
}
throw handledError;
}
}

Expand Down Expand Up @@ -208,13 +238,32 @@ class BraveSearch {
if (axios.isAxiosError(error)) {
const status = error.response?.status;
const message = error.response?.data?.message || error.message;
const responseData = error.response?.data;

if (status === 429) {
return new BraveSearchError(`Rate limit exceeded: ${message}`);
return new BraveSearchError(
`Rate limit exceeded: ${message}`,
responseData,
);
} else if (status === 401) {
return new BraveSearchError(`Authentication error: ${message}`);
return new BraveSearchError(
`Authentication error: ${message}`,
responseData,
);
} else if (status === 422) {
// Handle the specific 422 error related to 'result_filter' param
console.warn(
"Received 422 error, possibly related to brave server error. Retrying with modified 'result_filter' option in request.",
);
return new BraveSearchError(
`API error (${status}): ${message}. Retrying with modified 'result_filter' option in request.`,
responseData,
);
} else {
return new BraveSearchError(`API error (${status}): ${message}`);
return new BraveSearchError(
`API error (${status}): ${message}`,
responseData,
);
}
}
return new BraveSearchError(`Unexpected error: ${error.message}`);
Expand Down

0 comments on commit 3bc6492

Please sign in to comment.