File size: 1,475 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
const QUEUE_FULL_ERROR_NAME = "QueueFullError";
const QUEUE_FULL_ERROR_MESSAGE_PREFIX = "Queue limit reached:";

export class QueueFullError extends Error {
  statusCode = 429;

  constructor(queueSize: number, queueLimit: number) {
    super(
      `Queue limit reached: your team has ${queueSize} jobs queued (limit: ${queueLimit}). Please wait for existing jobs to complete before adding more, or upgrade your plan for a higher limit. For more info, see https://docs.firecrawl.dev/rate-limits#concurrent-browser-limits`,
    );
    this.name = QUEUE_FULL_ERROR_NAME;
  }
}

function getStringField(
  error: Record<string, unknown>,
  fields: string[],
): string {
  for (const field of fields) {
    if (field in error && error[field] !== undefined && error[field] !== null) {
      return String(error[field]);
    }
  }

  return "";
}

export function isQueueFullError(error: unknown): boolean {
  const objectError =
    error && typeof error === "object"
      ? (error as Record<string, unknown>)
      : null;

  const errorName = objectError
    ? getStringField(objectError, ["name", "type"])
    : "";
  if (errorName === QUEUE_FULL_ERROR_NAME) {
    return true;
  }

  const errorMessage =
    error instanceof Error
      ? error.message
      : typeof error === "string"
        ? error
        : objectError
          ? getStringField(objectError, ["message", "value"])
          : "";

  return errorMessage.startsWith(QUEUE_FULL_ERROR_MESSAGE_PREFIX);
}