Skip to content

Commit

Permalink
fix(api): add error handing to Google Safe Browsing API client
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahsnider committed Sep 9, 2024
1 parent 67f16c2 commit 6b24a1e
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions apps/api/src/safe-browsing/safe-browsing.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Inject, Injectable } from '@nestjs/common';
import { Inject, Injectable, Logger } from '@nestjs/common';
import { captureException } from '@sentry/bun';
import convert from 'convert';
import { google, safebrowsing_v4 } from 'googleapis';
import type Ioredis from 'ioredis';
Expand All @@ -18,6 +19,7 @@ export class SafeBrowsingService {

private readonly safebrowsing: safebrowsing_v4.Safebrowsing;
private readonly apiKey: string;
private readonly logger = new Logger(SafeBrowsingService.name);

constructor(
@Inject(ConfigService) configService: ConfigService,
Expand All @@ -30,7 +32,21 @@ export class SafeBrowsingService {
this.apiKey = configService.googleApiKey;
}

async hasThreatMatches(url: URL): Promise<boolean> {
/** This method doesn't throw, and instead returns `undefined` if there's an error with Google's API. */
async hasThreatMatches(url: URL): Promise<boolean | undefined> {
try {
return await this.hasThreatMatchesThrowable(url);
} catch (error) {
// Log + report the error
captureException(error);
this.logger.error(error);

// If Google is having an outage, just ignore it
return undefined;
}
}

private async hasThreatMatchesThrowable(url: URL): Promise<boolean> {
const normalizedUrl = SafeBrowsingService.normalizeUrl(url);

const cached = await this.redis.get(SafeBrowsingService.REDIS_PREFIX + normalizedUrl);
Expand Down

0 comments on commit 6b24a1e

Please sign in to comment.