Skip to content

Open source alternative to Semrush and Ahrefs

Label DataForSEO upstream warnings with an event and endpoint (#900)

Carter Smith330f020c9d0c
diff --git a/src/server/lib/dataforseo/core.ts b/src/server/lib/dataforseo/core.ts
index a28cd791..8adff411 100644
--- a/src/server/lib/dataforseo/core.ts
+++ b/src/server/lib/dataforseo/core.ts
@@ -1,11 +1,12 @@
import { AppError } from "@/server/lib/errors";
import { getRequiredEnvValue } from "@/server/lib/runtime-env";
import type { ErrorCode } from "@/shared/error-codes";
-// Type-only: erased at compile, so no runtime cycle with envelope.ts (which
-// imports DataforseoErrorClassifier from here the same way).
-import type {
- DataforseoResponseLike,
- DataforseoTaskLike,
+// No runtime cycle: envelope.ts imports only the DataforseoErrorClassifier
+// type from here.
+import {
+ warnIfUpstreamResponseFailed,
+ type DataforseoResponseLike,
+ type DataforseoTaskLike,
} from "@/server/lib/dataforseo/envelope";
const API_BASE = "https://api.dataforseo.com";
@@ -143,6 +144,7 @@ function createAuthenticatedFetch(
// the app to fix.
if (code === "UPSTREAM_UNAVAILABLE")
console.warn("dataforseo.upstream-http-failed", {
+ event: "dataforseo.upstream-http-failed",
path,
status: response.status,
});
@@ -187,7 +189,9 @@ async function requestDataforseo<TTask extends DataforseoTaskLike>(
const text = await response.text();
if (text === "") return null;
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the task type is the caller's claim about the payload; billing metadata and item fields are validated downstream (envelope.ts + section Zod schemas)
- return JSON.parse(text) as DataforseoResponseLike<TTask>;
+ const parsed = JSON.parse(text) as DataforseoResponseLike<TTask>;
+ warnIfUpstreamResponseFailed(parsed, path);
+ return parsed;
}
/**
diff --git a/src/server/lib/dataforseo/envelope.test.ts b/src/server/lib/dataforseo/envelope.test.ts
index 8d4c537a..1f71497f 100644
--- a/src/server/lib/dataforseo/envelope.test.ts
+++ b/src/server/lib/dataforseo/envelope.test.ts
@@ -71,7 +71,6 @@ describe("assertOk", () => {
);
it("classifies a top-level 50000 'Internal Server Error.' as UPSTREAM_UNAVAILABLE", () => {
- vi.spyOn(console, "warn").mockImplementation(() => {});
expect(() =>
assertOk({
status_code: 50000,
diff --git a/src/server/lib/dataforseo/envelope.ts b/src/server/lib/dataforseo/envelope.ts
index 46fbe36f..e9884b12 100644
--- a/src/server/lib/dataforseo/envelope.ts
+++ b/src/server/lib/dataforseo/envelope.ts
@@ -191,15 +191,32 @@ export function failedResponseError<T extends DataforseoTaskLike>(
fallbackMessage: string,
): AppError {
const message = response?.status_message || fallbackMessage;
- if (!isUpstreamFailureStatus(response?.status_code)) {
- return new AppError("INTERNAL_ERROR", message);
- }
- // Warn for the same reason as the task-level branch in assertOk.
+ return new AppError(
+ isUpstreamFailureStatus(response?.status_code)
+ ? "UPSTREAM_UNAVAILABLE"
+ : "INTERNAL_ERROR",
+ message,
+ );
+}
+
+/**
+ * Warns when DataForSEO's own backend failed the whole request, for the same
+ * reason as the task-level branch in assertOk. Called where the response is
+ * parsed, since only the request knows its endpoint: a failed response carries
+ * no task path. Observability shows the object's `message` in place of the
+ * label, so the label is repeated as `event`.
+ */
+export function warnIfUpstreamResponseFailed(
+ response: DataforseoResponseLike<DataforseoTaskLike> | null,
+ path: string,
+): void {
+ if (!isUpstreamFailureStatus(response?.status_code)) return;
console.warn("dataforseo.upstream-response-failed", {
+ event: "dataforseo.upstream-response-failed",
+ path,
status: response?.status_code,
- message,
+ message: response?.status_message,
});
- return new AppError("UPSTREAM_UNAVAILABLE", message);
}
/** Task lifecycle codes meaning "not done yet": Task Created / Task Handed /
@@ -274,8 +291,11 @@ export function assertOk<T extends DataforseoTaskLike>(
// what they capture, so warn here to keep the provider's failure rate — and
// the only remaining record of the message — visible in Workers
// Observability. Warn, not error: there is nothing in the app to fix.
+ // Observability shows the object's `message` in place of the label, so the
+ // label is repeated as `event`.
if (isUpstreamFailure)
console.warn("dataforseo.upstream-task-failed", {
+ event: "dataforseo.upstream-task-failed",
path,
status: task.status_code,
message: task.status_message,
diff --git a/src/server/lib/dataforseo/lighthouse.ts b/src/server/lib/dataforseo/lighthouse.ts
index 7653f4a3..35cb3d87 100644
--- a/src/server/lib/dataforseo/lighthouse.ts
+++ b/src/server/lib/dataforseo/lighthouse.ts
@@ -6,6 +6,7 @@ import {
type DataforseoApiResponse,
type DataforseoResponseLike,
type DataforseoTaskLike,
+ warnIfUpstreamResponseFailed,
} from "@/server/lib/dataforseo/envelope";
import {
parseDataforseoLighthousePayload,
@@ -63,6 +64,7 @@ export async function fetchLighthouseResult(input: {
return withParseLock(async () => {
const body =
await response.json<DataforseoResponseLike<DataforseoTaskLike>>();
+ warnIfUpstreamResponseFailed(body, LIGHTHOUSE_PATH);
// Build the metering envelope before parsing. The provider has already
// charged a successful task, so a malformed payload must carry its
// billing metadata out to the metered client instead of looking