Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(map): include id and propagate status when an upstream resolver fails (#5488)

Abimael Martell1b83b35821a1
diff --git a/apps/api/src/controllers/v2/map.ts b/apps/api/src/controllers/v2/map.ts
index 5eb32af53..fa7f50dd4 100644
--- a/apps/api/src/controllers/v2/map.ts
+++ b/apps/api/src/controllers/v2/map.ts
@@ -10,7 +10,7 @@ import { billTeam } from "../../services/billing/credit_billing";
import { logMap, logRequest } from "../../services/logging/log_job";
import { externalRequestId } from "../../lib/external-request-id";
import { logger as _logger } from "../../lib/logger";
-import { MapTimeoutError, MapFailedError } from "../../lib/error";
+import { MapTimeoutError } from "../../lib/error";
import { checkPermissions } from "../../lib/permissions";
import {
resolveSafeMode,
@@ -20,7 +20,7 @@ import { getMapResults, MapResult } from "../../lib/map-utils";
import { v7 as uuidv7 } from "uuid";
import { isBaseDomain, extractBaseDomain } from "../../lib/url-utils";
import { getScrapeZDR } from "../../lib/zdr-helpers";
-import { resolveViaAvgrab } from "../../lib/avgrab-resolve";
+import { AvgrabResolveError, resolveViaAvgrab } from "../../lib/avgrab-resolve";
import {
checkUrlsAgainstThreatPolicy,
resolveThreatProtection,
@@ -187,9 +187,14 @@ export async function mapController(
});
}
} catch (error) {
- if (error instanceof MapFailedError) {
- return res.status(500).json({
+ if (error instanceof AvgrabResolveError) {
+ const status = [403, 404, 503].includes(error.upstreamStatus)
+ ? error.upstreamStatus
+ : 502;
+ return res.status(status).json({
success: false,
+ id: mapId,
+ code: error.code,
error: error.message,
});
}
diff --git a/apps/api/src/controllers/v2/types.ts b/apps/api/src/controllers/v2/types.ts
index aa57549c3..270262f0f 100644
--- a/apps/api/src/controllers/v2/types.ts
+++ b/apps/api/src/controllers/v2/types.ts
@@ -2037,7 +2037,7 @@ export interface MapDocument {
// V2 Map Response with dictionary format
export type MapResponse =
- | ErrorResponse
+ | (ErrorResponse & { id?: string })
| {
success: true;
id: string;
diff --git a/apps/api/src/lib/avgrab-resolve.ts b/apps/api/src/lib/avgrab-resolve.ts
index e48b852d2..484f55f42 100644
--- a/apps/api/src/lib/avgrab-resolve.ts
+++ b/apps/api/src/lib/avgrab-resolve.ts
@@ -52,6 +52,15 @@ async function matchesResolveRegex(url: string): Promise<boolean> {
}
}
+export class AvgrabResolveError extends MapFailedError {
+ constructor(
+ message: string,
+ public readonly upstreamStatus: number,
+ ) {
+ super(message);
+ }
+}
+
interface AvgrabResolvedPost {
url: string;
title: string;
@@ -98,7 +107,7 @@ export async function resolveViaAvgrab(
status: response.status,
detail,
});
- throw new MapFailedError(detail);
+ throw new AvgrabResolveError(detail, response.status);
}
const data = (await response.json()) as AvgrabResolveResponse;