Skip to content

Commit

Permalink
fix: logging for search error
Browse files Browse the repository at this point in the history
  • Loading branch information
nomagick committed May 16, 2024
1 parent e0e37ad commit 8ec8c1e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
39 changes: 25 additions & 14 deletions backend/functions/src/cloud-functions/searcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,20 +451,31 @@ ${this.content}
}
}

const r = await this.braveSearchService.webSearch(query);

const nowDate = new Date();
const record = SearchResult.from({
query,
queryDigest,
response: r,
createdAt: nowDate,
expireAt: new Date(nowDate.valueOf() + this.cacheRetentionMs)
});
SearchResult.save(record.degradeForFireStore()).catch((err) => {
this.logger.warn(`Failed to cache search result`, { err });
});
try {
const r = await this.braveSearchService.webSearch(query);

const nowDate = new Date();
const record = SearchResult.from({
query,
queryDigest,
response: r,
createdAt: nowDate,
expireAt: new Date(nowDate.valueOf() + this.cacheRetentionMs)
});
SearchResult.save(record.degradeForFireStore()).catch((err) => {
this.logger.warn(`Failed to cache search result`, { err });
});

return r;
} catch (err: any) {
if (cache) {
this.logger.warn(`Failed to fetch search result, but a stale cache is available. falling back to stale cache`, { err: marshalErrorLike(err) });

return cache.response as WebSearchApiResponse;
}

throw err;
}

return r;
}
}
8 changes: 5 additions & 3 deletions backend/functions/src/services/brave-search.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AsyncService, DownstreamServiceFailureError } from 'civkit';
import { AsyncService, DownstreamServiceFailureError, marshalErrorLike } from 'civkit';
import { singleton } from 'tsyringe';
import { Logger } from '../shared/services/logger';
import { SecretExposer } from '../shared/services/secrets';
Expand Down Expand Up @@ -62,8 +62,10 @@ export class BraveSearchService extends AsyncService {
const r = await this.braveSearchHTTP.webSearch(query, { headers: extraHeaders as Record<string, string> });

return r.parsed;
} catch (err) {
throw new DownstreamServiceFailureError({ message: `Search failed`, cause: err });
} catch (err: any) {
this.logger.error(`Web search failed: ${err?.message}`, { err: marshalErrorLike(err) });

throw new DownstreamServiceFailureError({ message: `Search failed` });
}

}
Expand Down

0 comments on commit 8ec8c1e

Please sign in to comment.