You've already forked domainstack.io
mirror of
https://github.com/jakejarvis/domainstack.io.git
synced 2025-12-02 19:33:48 -05:00
refactor: enhance error logging structure across multiple modules for consistency
This commit is contained in:
@@ -15,7 +15,7 @@ const handler = async (req: Request) => {
|
||||
onError: async ({ path, error }) => {
|
||||
// Use logger for unhandled errors
|
||||
const { logger } = await import("@/lib/logger/server");
|
||||
logger.error(`[trpc] unhandled error ${path}`, error, { path });
|
||||
logger.error("unhandled error", error, { source: "trpc", path });
|
||||
},
|
||||
responseMeta: () => {
|
||||
// Add correlation ID to response headers for client tracking
|
||||
|
||||
@@ -14,14 +14,11 @@ export const onRequestError: Instrumentation.onRequestError = async (
|
||||
try {
|
||||
// Use logger for structured error logging
|
||||
const { logger } = await import("@/lib/logger/server");
|
||||
logger.error(
|
||||
"[instrumentation] request error",
|
||||
error instanceof Error ? error : new Error(String(error)),
|
||||
{
|
||||
path: request.path,
|
||||
method: request.method,
|
||||
},
|
||||
);
|
||||
logger.error("request error", error, {
|
||||
source: "instrumentation",
|
||||
path: request.path,
|
||||
method: request.method,
|
||||
});
|
||||
} catch {
|
||||
// Graceful degradation - don't throw to avoid breaking the request
|
||||
}
|
||||
|
||||
10
lib/blob.ts
10
lib/blob.ts
@@ -47,13 +47,9 @@ export async function deleteBlobs(urls: string[]): Promise<DeleteResult> {
|
||||
results.push({ url, deleted: true });
|
||||
} catch (err) {
|
||||
const message = (err as Error)?.message || "unknown";
|
||||
logger.error(
|
||||
"delete failed",
|
||||
err instanceof Error ? err : new Error(String(err)),
|
||||
{
|
||||
url,
|
||||
},
|
||||
);
|
||||
logger.error("delete failed", err, {
|
||||
url,
|
||||
});
|
||||
results.push({ url, deleted: false, error: message });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -188,7 +188,6 @@ async function ensureUrlAllowed(
|
||||
) {
|
||||
logger.warn("blocked host", {
|
||||
url: url.toString(),
|
||||
reason: "host_blocked",
|
||||
});
|
||||
throw new RemoteAssetError("host_blocked", `Host ${hostname} is blocked`);
|
||||
}
|
||||
@@ -199,7 +198,6 @@ async function ensureUrlAllowed(
|
||||
) {
|
||||
logger.warn("blocked host", {
|
||||
url: url.toString(),
|
||||
reason: "host_not_allowed",
|
||||
});
|
||||
throw new RemoteAssetError(
|
||||
"host_not_allowed",
|
||||
@@ -209,9 +207,8 @@ async function ensureUrlAllowed(
|
||||
|
||||
if (isIP(hostname)) {
|
||||
if (isBlockedIp(hostname)) {
|
||||
logger.warn("blocked ip", {
|
||||
logger.warn("blocked private ip", {
|
||||
url: url.toString(),
|
||||
reason: "private_ip",
|
||||
});
|
||||
throw new RemoteAssetError(
|
||||
"private_ip",
|
||||
@@ -225,9 +222,8 @@ async function ensureUrlAllowed(
|
||||
try {
|
||||
records = await dnsLookup(hostname, { all: true });
|
||||
} catch (err) {
|
||||
logger.warn("dns error", {
|
||||
logger.error("unexpected lookup error", err, {
|
||||
url: url.toString(),
|
||||
reason: err instanceof Error ? err.message : "dns_error",
|
||||
});
|
||||
throw new RemoteAssetError(
|
||||
"dns_error",
|
||||
@@ -236,17 +232,15 @@ async function ensureUrlAllowed(
|
||||
}
|
||||
|
||||
if (!records || records.length === 0) {
|
||||
logger.warn("dns error", {
|
||||
logger.warn("lookup returned no records", {
|
||||
url: url.toString(),
|
||||
reason: "no_records",
|
||||
});
|
||||
throw new RemoteAssetError("dns_error", "DNS lookup returned no records");
|
||||
}
|
||||
|
||||
if (records.some((record) => isBlockedIp(record.address))) {
|
||||
logger.warn("blocked ip", {
|
||||
logger.warn("blocked private ip", {
|
||||
url: url.toString(),
|
||||
reason: "private_ip",
|
||||
});
|
||||
throw new RemoteAssetError(
|
||||
"private_ip",
|
||||
|
||||
17
trpc/init.ts
17
trpc/init.ts
@@ -71,10 +71,11 @@ const withLogging = t.middleware(async ({ path, type, input, next }) => {
|
||||
const { logger } = await import("@/lib/logger/server");
|
||||
|
||||
// Log procedure start
|
||||
logger.debug(`[trpc] start ${path}`, {
|
||||
logger.debug("procedure start", {
|
||||
source: "trpc",
|
||||
path,
|
||||
type,
|
||||
...(input && typeof input === "object" ? { ...input } : {}),
|
||||
input: input && typeof input === "object" ? { ...input } : undefined,
|
||||
});
|
||||
|
||||
try {
|
||||
@@ -82,10 +83,12 @@ const withLogging = t.middleware(async ({ path, type, input, next }) => {
|
||||
const durationMs = Math.round(performance.now() - start);
|
||||
|
||||
// Log successful completion
|
||||
logger.info(`[trpc] ok ${path}`, {
|
||||
logger.debug("procedure ok", {
|
||||
source: "trpc",
|
||||
path,
|
||||
type,
|
||||
durationMs,
|
||||
input: input && typeof input === "object" ? { ...input } : undefined,
|
||||
});
|
||||
|
||||
// Track slow requests (>5s threshold) in PostHog
|
||||
@@ -101,15 +104,16 @@ const withLogging = t.middleware(async ({ path, type, input, next }) => {
|
||||
return result;
|
||||
} catch (err) {
|
||||
const durationMs = Math.round(performance.now() - start);
|
||||
const error = err instanceof Error ? err : new Error(String(err));
|
||||
|
||||
// Log error with full details
|
||||
logger.error(`[trpc] error ${path}`, error, {
|
||||
logger.error("procedure error", err, {
|
||||
source: "trpc",
|
||||
path,
|
||||
type,
|
||||
durationMs,
|
||||
});
|
||||
|
||||
// Re-throw the error to be handled by the error boundary
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
@@ -130,7 +134,8 @@ const withDomainAccessUpdate = t.middleware(async ({ input, next }) => {
|
||||
const registrable = toRegistrableDomain(input.domain);
|
||||
if (registrable) {
|
||||
const { logger } = await import("@/lib/logger/server");
|
||||
logger.debug("[trpc] recording access for domain", {
|
||||
logger.info("recording access for domain", {
|
||||
source: "trpc",
|
||||
domain: registrable,
|
||||
});
|
||||
after(() => updateLastAccessed(registrable));
|
||||
|
||||
Reference in New Issue
Block a user