fix: logging for search error

This commit is contained in:
yanlong.wang 2024-05-16 19:01:30 +08:00
parent e0e37ad4d7
commit 8ec8c1e718
No known key found for this signature in database
GPG Key ID: C0A623C0BADF9F37
2 changed files with 29 additions and 16 deletions

View File

@ -451,20 +451,31 @@ ${this.content}
}
}
const r = await this.braveSearchService.webSearch(query);
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 });
});
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;
}
}

View File

@ -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';
@ -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` });
}
}