File size: 2,142 Bytes
097eaf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import type { Logger } from "winston";

const NATIVE_LOGS_SEPARATOR = "\n__native_logs__:";

/** Matches the NativeLogEntry struct from Rust (@mendable/firecrawl-rs). */
interface NativeLogEntry {
  level: string;
  target: string;
  message: string;
  fields: Record<string, unknown>;
  timestampMs: number;
}

/**
 * Extract native logs embedded in a NAPI error message by `embed_logs_in_error`.
 * Emits them through the logger and returns the cleaned error message.
 */
export function extractAndEmitNativeLogs(
  error: unknown,
  parentLogger: Logger,
  module: string,
): void {
  if (!(error instanceof Error)) return;
  const idx = error.message.indexOf(NATIVE_LOGS_SEPARATOR);
  if (idx === -1) return;

  const logsJson = error.message.slice(idx + NATIVE_LOGS_SEPARATOR.length);

  try {
    const logs: NativeLogEntry[] = JSON.parse(logsJson);
    // Only strip after successful parse so we don't lose data on failure
    error.message = error.message.slice(0, idx);
    emitNativeLogs(logs, parentLogger, module);
  } catch {
    // JSON parse failed — leave the original error message intact
  }
}

/**
 * Emit log entries captured inside the Rust native module through a Winston
 * logger, preserving trace context (scrape_id / url via the parent logger)
 * and adding `source: "native"` + the Rust module name as labels.
 */
export function emitNativeLogs(
  logs: NativeLogEntry[] | undefined,
  parentLogger: Logger,
  module: string,
): void {
  if (!logs || logs.length === 0) return;

  const childLogger = parentLogger.child({ source: "native", module });

  for (const entry of logs) {
    const meta = {
      rustTarget: entry.target,
      ...entry.fields,
    };

    switch (entry.level) {
      case "error":
        childLogger.error(entry.message, meta);
        break;
      case "warn":
        childLogger.warn(entry.message, meta);
        break;
      case "info":
        childLogger.info(entry.message, meta);
        break;
      case "debug":
      case "trace":
        childLogger.debug(entry.message, meta);
        break;
      default:
        childLogger.info(entry.message, meta);
    }
  }
}