File size: 2,863 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { jest } from "@jest/globals";

const captureException = jest.fn();
const init = jest.fn();
const setTag = jest.fn();
const scopeSetExtra = jest.fn();
const scopeSetTag = jest.fn();

jest.mock("@sentry/node", () => ({
  captureException,
  getCurrentScope: () => ({
    setExtra: scopeSetExtra,
    setTag: scopeSetTag,
  }),
  init,
  setTag,
  vercelAIIntegration: jest.fn(() => ({})),
}));

jest.mock("../config", () => ({
  config: {
    NUQ_POD_NAME: "test-pod",
    SENTRY_DSN: undefined,
    SENTRY_ENVIRONMENT: "test",
    SENTRY_ERROR_SAMPLE_RATE: 1,
    SENTRY_TRACE_SAMPLE_RATE: 0,
  },
}));

jest.mock("../lib/logger", () => ({
  logger: {
    info: jest.fn(),
  },
}));

class AddFeatureError extends Error {}
class RemoveFeatureError extends Error {}
class EngineError extends Error {}
class AbortManagerThrownError extends Error {}
class JobCancelledError extends Error {}

jest.mock("../scraper/scrapeURL/error", () => ({
  AddFeatureError,
  RemoveFeatureError,
  EngineError,
}));

jest.mock("../scraper/scrapeURL/lib/abortManager", () => ({
  AbortManagerThrownError,
}));

jest.mock("../lib/error", () => ({
  JobCancelledError,
}));

import { QueueFullError } from "../lib/queue-full-error";
import {
  captureExceptionWithZdrCheck,
  shouldIgnoreSentryException,
} from "./sentry";

describe("sentry filtering", () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  it("does not capture queue full errors", () => {
    captureExceptionWithZdrCheck(new QueueFullError(50000, 50000));

    expect(captureException).not.toHaveBeenCalled();
  });

  it("recognizes Sentry exception payloads for queue full errors", () => {
    expect(
      shouldIgnoreSentryException({
        type: "QueueFullError",
        value:
          "Queue limit reached: your team has 50000 jobs queued (limit: 50000).",
      }),
    ).toBe(true);
  });

  it("recognizes serialized transportable errors in Sentry exception values", () => {
    expect(
      shouldIgnoreSentryException({
        type: "Error",
        value: 'SCRAPE_TIMEOUT|{"message":"timeout"}',
      }),
    ).toBe(true);
  });

  it("recognizes cancellation errors in Sentry exception values", () => {
    expect(
      shouldIgnoreSentryException({
        type: "Error",
        value: "Parent crawl/batch scrape was cancelled",
      }),
    ).toBe(true);
  });

  it("does not ignore unexpected Sentry exception values", () => {
    expect(
      shouldIgnoreSentryException({
        type: "Error",
        value: "boom",
      }),
    ).toBe(false);
  });

  it("still captures unexpected errors", () => {
    const error = new Error("boom");

    captureExceptionWithZdrCheck(error, {
      tags: { module: "test" },
      zeroDataRetention: false,
    });

    expect(captureException).toHaveBeenCalledWith(error, {
      tags: { module: "test" },
    });
  });
});