11import axios from "axios" ;
2-
32import { getDbDomain } from "../functions/db/getDbDomain" ;
43import { prisma } from "../prisma" ;
54
@@ -12,7 +11,7 @@ export class SecurityTrailsService {
1211 * Asynchronously checks a given domain against the SecurityTrails service for any known bad domains.
1312 *
1413 * @param {string } domain - The domain name to be checked.
15- * @returns
14+ * @returns { Promise<any> } - The API response or a fallback if a rate limit error occurs.
1615 */
1716 check : async ( domain : string ) => {
1817 // metrics.increment("services.securitytrails.domain.check");
@@ -26,23 +25,39 @@ export class SecurityTrailsService {
2625 } ,
2726 } ;
2827
29- const response = await axios . request ( options ) ;
30- const data = response . data ;
31- const dbDomain = await getDbDomain ( domain ) ;
28+ try {
29+ const response = await axios . request ( options ) ;
30+ const data = response . data ;
31+ const dbDomain = await getDbDomain ( domain ) ;
3232
33- await prisma . rawAPIData . create ( {
34- data : {
35- sourceAPI : "SecurityTrails" ,
36- domain : {
37- connect : {
38- id : dbDomain . id ,
33+ await prisma . rawAPIData . create ( {
34+ data : {
35+ sourceAPI : "SecurityTrails" ,
36+ domain : {
37+ connect : {
38+ id : dbDomain . id ,
39+ } ,
3940 } ,
41+ data : data ,
4042 } ,
41- data : data ,
42- } ,
43- } ) ;
43+ } ) ;
44+
45+ return data ;
46+ } catch ( error : any ) {
47+ if ( error . response ?. status === 429 ) {
48+ console . warn (
49+ `Rate limit exceeded for domain: ${ domain } . Returning empty result.`
50+ ) ;
51+ // Handle 429 error (Too Many Requests) by returning a fallback response.
52+ return {
53+ error : "Rate limit exceeded" ,
54+ retryAfter : error . response . headers [ "retry-after" ] || "unknown" ,
55+ } ;
56+ }
4457
45- return data ;
58+ // Re-throw other errors to avoid silently ignoring critical issues.
59+ throw error ;
60+ }
4661 } ,
4762 } ;
4863}
0 commit comments