File size: 2,569 Bytes
c09f67c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Custom error types for document extraction
 */

export class ExtractionError extends Error {
  constructor(
    message: string,
    public readonly code: string,
    public readonly cause?: Error,
  ) {
    super(message);
    this.name = "ExtractionError";
  }
}

export class ModelExtractionError extends ExtractionError {
  constructor(
    message: string,
    public readonly model: string,
    public readonly pass: number,
    cause?: Error,
  ) {
    super(message, "MODEL_EXTRACTION_ERROR", cause);
    this.name = "ModelExtractionError";
  }
}

export class QualityValidationError extends ExtractionError {
  constructor(
    message: string,
    public readonly qualityScore: number,
    public readonly threshold: number,
    public readonly missingFields: string[],
  ) {
    super(message, "QUALITY_VALIDATION_ERROR");
    this.name = "QualityValidationError";
  }
}

export class FieldReExtractionError extends ExtractionError {
  constructor(
    message: string,
    public readonly field: string,
    cause?: Error,
  ) {
    super(message, "FIELD_RE_EXTRACTION_ERROR", cause);
    this.name = "FieldReExtractionError";
  }
}

export class DocumentLoadError extends ExtractionError {
  constructor(message: string, cause?: Error) {
    super(message, "DOCUMENT_LOAD_ERROR", cause);
    this.name = "DocumentLoadError";
  }
}

/**
 * Check if an error is retryable (network/timeout errors)
 */
export function isRetryableError(error: unknown): boolean {
  if (!(error instanceof Error)) return false;

  const errorMessage = error.message.toLowerCase();
  const errorName = error.name;

  return (
    errorMessage.includes("timeout") ||
    errorMessage.includes("network") ||
    errorMessage.includes("503") ||
    errorMessage.includes("service unavailable") ||
    errorMessage.includes("aborted") ||
    errorName === "AbortError" ||
    errorName === "TimeoutError" ||
    (error instanceof DOMException && error.code === 23)
  );
}

/**
 * Extract error details for logging
 */
export function getErrorDetails(error: unknown): {
  message: string;
  name: string;
  code?: string;
  cause?: string;
  stack?: string;
} {
  if (error instanceof ExtractionError) {
    return {
      message: error.message,
      name: error.name,
      code: error.code,
      cause: error.cause?.message,
      stack: error.stack,
    };
  }

  if (error instanceof Error) {
    return {
      message: error.message,
      name: error.name,
      stack: error.stack,
    };
  }

  return {
    message: String(error),
    name: "UnknownError",
  };
}