pdf-oxide 0.3.24 → 0.3.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (271) hide show
  1. package/lib/builders/annotation-builder.d.ts +199 -0
  2. package/lib/builders/annotation-builder.d.ts.map +1 -0
  3. package/lib/builders/annotation-builder.js +318 -0
  4. package/lib/builders/annotation-builder.js.map +1 -0
  5. package/lib/builders/conversion-options-builder.d.ts +107 -0
  6. package/lib/builders/conversion-options-builder.d.ts.map +1 -0
  7. package/lib/builders/conversion-options-builder.js +215 -0
  8. package/lib/builders/conversion-options-builder.js.map +1 -0
  9. package/{src/builders/index.ts → lib/builders/index.d.ts} +1 -1
  10. package/lib/builders/index.d.ts.map +1 -0
  11. package/lib/builders/index.js +12 -0
  12. package/lib/builders/index.js.map +1 -0
  13. package/lib/builders/metadata-builder.d.ts +202 -0
  14. package/lib/builders/metadata-builder.d.ts.map +1 -0
  15. package/lib/builders/metadata-builder.js +286 -0
  16. package/lib/builders/metadata-builder.js.map +1 -0
  17. package/lib/builders/pdf-builder.d.ts +215 -0
  18. package/lib/builders/pdf-builder.d.ts.map +1 -0
  19. package/lib/builders/pdf-builder.js +339 -0
  20. package/lib/builders/pdf-builder.js.map +1 -0
  21. package/lib/builders/search-options-builder.d.ts +74 -0
  22. package/lib/builders/search-options-builder.d.ts.map +1 -0
  23. package/lib/builders/search-options-builder.js +130 -0
  24. package/lib/builders/search-options-builder.js.map +1 -0
  25. package/lib/document-editor-manager.d.ts +140 -0
  26. package/lib/document-editor-manager.d.ts.map +1 -0
  27. package/lib/document-editor-manager.js +257 -0
  28. package/lib/document-editor-manager.js.map +1 -0
  29. package/lib/errors.d.ts +383 -0
  30. package/lib/errors.d.ts.map +1 -0
  31. package/lib/errors.js +1117 -0
  32. package/lib/errors.js.map +1 -0
  33. package/lib/form-field-manager.d.ts +300 -0
  34. package/lib/form-field-manager.d.ts.map +1 -0
  35. package/lib/form-field-manager.js +567 -0
  36. package/lib/form-field-manager.js.map +1 -0
  37. package/lib/hybrid-ml-manager.d.ts +143 -0
  38. package/lib/hybrid-ml-manager.d.ts.map +1 -0
  39. package/lib/hybrid-ml-manager.js +209 -0
  40. package/lib/hybrid-ml-manager.js.map +1 -0
  41. package/lib/index.d.ts +23 -0
  42. package/lib/index.d.ts.map +1 -0
  43. package/lib/index.js +280 -0
  44. package/lib/index.js.map +1 -0
  45. package/lib/managers/accessibility-manager.d.ts +149 -0
  46. package/lib/managers/accessibility-manager.d.ts.map +1 -0
  47. package/lib/managers/accessibility-manager.js +224 -0
  48. package/lib/managers/accessibility-manager.js.map +1 -0
  49. package/lib/managers/annotation-manager.d.ts +220 -0
  50. package/lib/managers/annotation-manager.d.ts.map +1 -0
  51. package/lib/managers/annotation-manager.js +360 -0
  52. package/lib/managers/annotation-manager.js.map +1 -0
  53. package/lib/managers/barcode-manager.d.ts +80 -0
  54. package/lib/managers/barcode-manager.d.ts.map +1 -0
  55. package/lib/managers/barcode-manager.js +226 -0
  56. package/lib/managers/barcode-manager.js.map +1 -0
  57. package/lib/managers/batch-manager.d.ts +186 -0
  58. package/lib/managers/batch-manager.d.ts.map +1 -0
  59. package/lib/managers/batch-manager.js +389 -0
  60. package/lib/managers/batch-manager.js.map +1 -0
  61. package/lib/managers/cache-manager.d.ts +182 -0
  62. package/lib/managers/cache-manager.d.ts.map +1 -0
  63. package/lib/managers/cache-manager.js +387 -0
  64. package/lib/managers/cache-manager.js.map +1 -0
  65. package/lib/managers/compliance-manager.d.ts +104 -0
  66. package/lib/managers/compliance-manager.d.ts.map +1 -0
  67. package/lib/managers/compliance-manager.js +415 -0
  68. package/lib/managers/compliance-manager.js.map +1 -0
  69. package/lib/managers/content-manager.d.ts +121 -0
  70. package/lib/managers/content-manager.d.ts.map +1 -0
  71. package/lib/managers/content-manager.js +295 -0
  72. package/lib/managers/content-manager.js.map +1 -0
  73. package/lib/managers/document-utility-manager.d.ts +370 -0
  74. package/lib/managers/document-utility-manager.d.ts.map +1 -0
  75. package/lib/managers/document-utility-manager.js +731 -0
  76. package/lib/managers/document-utility-manager.js.map +1 -0
  77. package/lib/managers/dom-pdf-creator.d.ts +105 -0
  78. package/lib/managers/dom-pdf-creator.d.ts.map +1 -0
  79. package/lib/managers/dom-pdf-creator.js +300 -0
  80. package/lib/managers/dom-pdf-creator.js.map +1 -0
  81. package/lib/managers/editing-manager.d.ts +249 -0
  82. package/lib/managers/editing-manager.d.ts.map +1 -0
  83. package/lib/managers/editing-manager.js +388 -0
  84. package/lib/managers/editing-manager.js.map +1 -0
  85. package/lib/managers/enterprise-manager.d.ts +193 -0
  86. package/lib/managers/enterprise-manager.d.ts.map +1 -0
  87. package/lib/managers/enterprise-manager.js +305 -0
  88. package/lib/managers/enterprise-manager.js.map +1 -0
  89. package/lib/managers/extended-managers.d.ts +123 -0
  90. package/lib/managers/extended-managers.d.ts.map +1 -0
  91. package/lib/managers/extended-managers.js +658 -0
  92. package/lib/managers/extended-managers.js.map +1 -0
  93. package/lib/managers/extraction-manager.d.ts +247 -0
  94. package/lib/managers/extraction-manager.d.ts.map +1 -0
  95. package/lib/managers/extraction-manager.js +478 -0
  96. package/lib/managers/extraction-manager.js.map +1 -0
  97. package/lib/managers/final-utilities.d.ts +128 -0
  98. package/lib/managers/final-utilities.d.ts.map +1 -0
  99. package/lib/managers/final-utilities.js +653 -0
  100. package/lib/managers/final-utilities.js.map +1 -0
  101. package/lib/managers/hybrid-ml-advanced.d.ts +137 -0
  102. package/lib/managers/hybrid-ml-advanced.d.ts.map +1 -0
  103. package/lib/managers/hybrid-ml-advanced.js +707 -0
  104. package/lib/managers/hybrid-ml-advanced.js.map +1 -0
  105. package/lib/managers/index.d.ts +65 -0
  106. package/lib/managers/index.d.ts.map +1 -0
  107. package/lib/managers/index.js +70 -0
  108. package/lib/managers/index.js.map +1 -0
  109. package/lib/managers/layer-manager.d.ts +204 -0
  110. package/lib/managers/layer-manager.d.ts.map +1 -0
  111. package/lib/managers/layer-manager.js +403 -0
  112. package/lib/managers/layer-manager.js.map +1 -0
  113. package/lib/managers/metadata-manager.d.ts +149 -0
  114. package/lib/managers/metadata-manager.d.ts.map +1 -0
  115. package/lib/managers/metadata-manager.js +281 -0
  116. package/lib/managers/metadata-manager.js.map +1 -0
  117. package/lib/managers/ocr-manager.d.ts +195 -0
  118. package/lib/managers/ocr-manager.d.ts.map +1 -0
  119. package/lib/managers/ocr-manager.js +583 -0
  120. package/lib/managers/ocr-manager.js.map +1 -0
  121. package/lib/managers/optimization-manager.d.ts +103 -0
  122. package/lib/managers/optimization-manager.d.ts.map +1 -0
  123. package/lib/managers/optimization-manager.js +194 -0
  124. package/lib/managers/optimization-manager.js.map +1 -0
  125. package/lib/managers/outline-manager.d.ts +102 -0
  126. package/lib/managers/outline-manager.d.ts.map +1 -0
  127. package/lib/managers/outline-manager.js +170 -0
  128. package/lib/managers/outline-manager.js.map +1 -0
  129. package/lib/managers/page-manager.d.ts +143 -0
  130. package/lib/managers/page-manager.d.ts.map +1 -0
  131. package/lib/managers/page-manager.js +237 -0
  132. package/lib/managers/page-manager.js.map +1 -0
  133. package/lib/managers/pattern-detection.d.ts +170 -0
  134. package/lib/managers/pattern-detection.d.ts.map +1 -0
  135. package/lib/managers/pattern-detection.js +325 -0
  136. package/lib/managers/pattern-detection.js.map +1 -0
  137. package/lib/managers/rendering-manager.d.ts +354 -0
  138. package/lib/managers/rendering-manager.d.ts.map +1 -0
  139. package/lib/managers/rendering-manager.js +680 -0
  140. package/lib/managers/rendering-manager.js.map +1 -0
  141. package/lib/managers/search-manager.d.ts +236 -0
  142. package/lib/managers/search-manager.d.ts.map +1 -0
  143. package/lib/managers/search-manager.js +330 -0
  144. package/lib/managers/search-manager.js.map +1 -0
  145. package/lib/managers/security-manager.d.ts +162 -0
  146. package/lib/managers/security-manager.d.ts.map +1 -0
  147. package/lib/managers/security-manager.js +293 -0
  148. package/lib/managers/security-manager.js.map +1 -0
  149. package/lib/managers/signature-manager.d.ts +725 -0
  150. package/lib/managers/signature-manager.d.ts.map +1 -0
  151. package/lib/managers/signature-manager.js +1365 -0
  152. package/lib/managers/signature-manager.js.map +1 -0
  153. package/lib/managers/streams.d.ts +263 -0
  154. package/lib/managers/streams.d.ts.map +1 -0
  155. package/lib/managers/streams.js +472 -0
  156. package/lib/managers/streams.js.map +1 -0
  157. package/lib/managers/xfa-manager.d.ts +228 -0
  158. package/lib/managers/xfa-manager.d.ts.map +1 -0
  159. package/lib/managers/xfa-manager.js +490 -0
  160. package/lib/managers/xfa-manager.js.map +1 -0
  161. package/lib/pdf-creator-manager.d.ts +201 -0
  162. package/lib/pdf-creator-manager.d.ts.map +1 -0
  163. package/lib/pdf-creator-manager.js +379 -0
  164. package/lib/pdf-creator-manager.js.map +1 -0
  165. package/lib/properties.d.ts +80 -0
  166. package/lib/properties.d.ts.map +1 -0
  167. package/lib/properties.js +455 -0
  168. package/lib/properties.js.map +1 -0
  169. package/lib/result-accessors-manager.d.ts +347 -0
  170. package/lib/result-accessors-manager.d.ts.map +1 -0
  171. package/lib/result-accessors-manager.js +705 -0
  172. package/lib/result-accessors-manager.js.map +1 -0
  173. package/lib/thumbnail-manager.d.ts +122 -0
  174. package/lib/thumbnail-manager.d.ts.map +1 -0
  175. package/lib/thumbnail-manager.js +206 -0
  176. package/lib/thumbnail-manager.js.map +1 -0
  177. package/lib/types/common.d.ts +93 -0
  178. package/lib/types/common.d.ts.map +1 -0
  179. package/lib/types/common.js +5 -0
  180. package/lib/types/common.js.map +1 -0
  181. package/lib/types/document-types.d.ts +353 -0
  182. package/lib/types/document-types.d.ts.map +1 -0
  183. package/lib/types/document-types.js +83 -0
  184. package/lib/types/document-types.js.map +1 -0
  185. package/{src/types/index.ts → lib/types/index.d.ts} +1 -1
  186. package/lib/types/index.d.ts.map +1 -0
  187. package/lib/types/index.js +6 -0
  188. package/lib/types/index.js.map +1 -0
  189. package/lib/types/manager-types.d.ts +180 -0
  190. package/lib/types/manager-types.d.ts.map +1 -0
  191. package/lib/types/manager-types.js +99 -0
  192. package/lib/types/manager-types.js.map +1 -0
  193. package/lib/types/native-bindings.d.ts +440 -0
  194. package/lib/types/native-bindings.d.ts.map +1 -0
  195. package/lib/types/native-bindings.js +8 -0
  196. package/lib/types/native-bindings.js.map +1 -0
  197. package/{src/workers/index.ts → lib/workers/index.d.ts} +1 -1
  198. package/lib/workers/index.d.ts.map +1 -0
  199. package/lib/workers/index.js +6 -0
  200. package/lib/workers/index.js.map +1 -0
  201. package/lib/workers/pool.d.ts +65 -0
  202. package/lib/workers/pool.d.ts.map +1 -0
  203. package/lib/workers/pool.js +195 -0
  204. package/lib/workers/pool.js.map +1 -0
  205. package/lib/workers/worker.d.ts +6 -0
  206. package/lib/workers/worker.d.ts.map +1 -0
  207. package/lib/workers/worker.js +100 -0
  208. package/lib/workers/worker.js.map +1 -0
  209. package/package.json +12 -22
  210. package/prebuilds/darwin-arm64/pdf_oxide.node +0 -0
  211. package/prebuilds/darwin-x64/pdf_oxide.node +0 -0
  212. package/prebuilds/linux-arm64/pdf_oxide.node +0 -0
  213. package/prebuilds/linux-x64/pdf_oxide.node +0 -0
  214. package/prebuilds/win32-x64/pdf_oxide.node +0 -0
  215. package/binding.gyp +0 -35
  216. package/src/builders/annotation-builder.ts +0 -367
  217. package/src/builders/conversion-options-builder.ts +0 -257
  218. package/src/builders/metadata-builder.ts +0 -317
  219. package/src/builders/pdf-builder.ts +0 -386
  220. package/src/builders/search-options-builder.ts +0 -151
  221. package/src/document-editor-manager.ts +0 -318
  222. package/src/errors.ts +0 -1629
  223. package/src/form-field-manager.ts +0 -666
  224. package/src/hybrid-ml-manager.ts +0 -283
  225. package/src/index.ts +0 -453
  226. package/src/managers/accessibility-manager.ts +0 -338
  227. package/src/managers/annotation-manager.ts +0 -439
  228. package/src/managers/barcode-manager.ts +0 -235
  229. package/src/managers/batch-manager.ts +0 -533
  230. package/src/managers/cache-manager.ts +0 -486
  231. package/src/managers/compliance-manager.ts +0 -375
  232. package/src/managers/content-manager.ts +0 -339
  233. package/src/managers/document-utility-manager.ts +0 -922
  234. package/src/managers/dom-pdf-creator.ts +0 -365
  235. package/src/managers/editing-manager.ts +0 -514
  236. package/src/managers/enterprise-manager.ts +0 -478
  237. package/src/managers/extended-managers.ts +0 -437
  238. package/src/managers/extraction-manager.ts +0 -583
  239. package/src/managers/final-utilities.ts +0 -429
  240. package/src/managers/hybrid-ml-advanced.ts +0 -479
  241. package/src/managers/index.ts +0 -239
  242. package/src/managers/layer-manager.ts +0 -500
  243. package/src/managers/metadata-manager.ts +0 -303
  244. package/src/managers/ocr-manager.ts +0 -756
  245. package/src/managers/optimization-manager.ts +0 -262
  246. package/src/managers/outline-manager.ts +0 -196
  247. package/src/managers/page-manager.ts +0 -289
  248. package/src/managers/pattern-detection.ts +0 -440
  249. package/src/managers/rendering-manager.ts +0 -863
  250. package/src/managers/search-manager.ts +0 -385
  251. package/src/managers/security-manager.ts +0 -345
  252. package/src/managers/signature-manager.ts +0 -1664
  253. package/src/managers/streams.ts +0 -618
  254. package/src/managers/xfa-manager.ts +0 -500
  255. package/src/pdf-creator-manager.ts +0 -494
  256. package/src/properties.ts +0 -522
  257. package/src/result-accessors-manager.ts +0 -867
  258. package/src/tests/advanced-features.test.ts +0 -414
  259. package/src/tests/advanced.test.ts +0 -266
  260. package/src/tests/extended-managers.test.ts +0 -316
  261. package/src/tests/final-utilities.test.ts +0 -455
  262. package/src/tests/foundation.test.ts +0 -315
  263. package/src/tests/high-demand.test.ts +0 -257
  264. package/src/tests/specialized.test.ts +0 -97
  265. package/src/thumbnail-manager.ts +0 -272
  266. package/src/types/common.ts +0 -142
  267. package/src/types/document-types.ts +0 -457
  268. package/src/types/manager-types.ts +0 -284
  269. package/src/types/native-bindings.ts +0 -517
  270. package/src/workers/pool.ts +0 -274
  271. package/src/workers/worker.ts +0 -131
package/lib/errors.js ADDED
@@ -0,0 +1,1117 @@
1
+ /**
2
+ * Unified Error Handling for PDF Oxide
3
+ *
4
+ * Provides comprehensive exception hierarchy consistent across all language bindings.
5
+ * Uses 4-digit error codes organized by category (1000-9999).
6
+ *
7
+ * Error Code System:
8
+ * - 1000-1999: Parse errors
9
+ * - 2000-2999: I/O errors
10
+ * - 3000-3999: Encryption errors
11
+ * - 4000-4999: State errors
12
+ * - 5000-5999: Unsupported feature errors
13
+ * - 6000-6999: Validation errors
14
+ * - 7000-7999: Rendering errors
15
+ * - 8000-8999: Search errors
16
+ * - 9000-9999: Other errors (compliance, OCR, etc.)
17
+ */
18
+ /**
19
+ * Error categories for classification and handling
20
+ */
21
+ export var ErrorCategory;
22
+ (function (ErrorCategory) {
23
+ ErrorCategory["VALIDATION"] = "validation";
24
+ ErrorCategory["IO"] = "io";
25
+ ErrorCategory["ENCRYPTION"] = "encryption";
26
+ ErrorCategory["PARSING"] = "parsing";
27
+ ErrorCategory["RENDERING"] = "rendering";
28
+ ErrorCategory["SEARCH"] = "search";
29
+ ErrorCategory["PERMISSION"] = "permission";
30
+ ErrorCategory["RESOURCE"] = "resource";
31
+ ErrorCategory["STATE"] = "state";
32
+ ErrorCategory["UNSUPPORTED"] = "unsupported";
33
+ ErrorCategory["COMPLIANCE"] = "compliance";
34
+ ErrorCategory["OCR"] = "ocr";
35
+ ErrorCategory["SIGNATURE"] = "signature";
36
+ ErrorCategory["REDACTION"] = "redaction";
37
+ ErrorCategory["UNKNOWN"] = "unknown";
38
+ })(ErrorCategory || (ErrorCategory = {}));
39
+ /**
40
+ * Error severity levels for prioritization and monitoring
41
+ */
42
+ export var ErrorSeverity;
43
+ (function (ErrorSeverity) {
44
+ ErrorSeverity["LOW"] = "low";
45
+ ErrorSeverity["MEDIUM"] = "medium";
46
+ ErrorSeverity["HIGH"] = "high";
47
+ ErrorSeverity["CRITICAL"] = "critical";
48
+ })(ErrorSeverity || (ErrorSeverity = {}));
49
+ /**
50
+ * Base class for all PDF Oxide errors.
51
+ *
52
+ * @class PdfException
53
+ * @extends {Error}
54
+ * @property {string} code - 4-digit error code (XXXX format)
55
+ * @property {string} message - Human-readable error message
56
+ * @property {PdfErrorDetails} details - Additional context information
57
+ *
58
+ * @example
59
+ * try {
60
+ * // PDF operation
61
+ * } catch (err) {
62
+ * if (err instanceof PdfException) {
63
+ * console.log(`[${err.code}] ${err.message}`);
64
+ * console.log('Context:', err.details);
65
+ * }
66
+ * }
67
+ */
68
+ export class PdfException extends Error {
69
+ /**
70
+ * Creates a new PdfException.
71
+ *
72
+ * @param code - 4-digit error code
73
+ * @param message - Human-readable error message
74
+ * @param category - Error category for classification
75
+ * @param severity - Error severity level
76
+ * @param details - Additional context information
77
+ * @param recovery - Recovery information
78
+ */
79
+ constructor(code, message, category = ErrorCategory.UNKNOWN, severity = ErrorSeverity.HIGH, details = {}, recovery = {}) {
80
+ if (!code || code.length !== 4 || !/^\d{4}$/.test(code)) {
81
+ throw new Error('Code must be 4 digits');
82
+ }
83
+ super(`[${code}] ${message}`);
84
+ this.name = this.constructor.name;
85
+ this.code = code;
86
+ this.message = message;
87
+ this.category = category;
88
+ this.severity = severity;
89
+ this.details = {
90
+ timestamp: new Date().toISOString(),
91
+ ...details,
92
+ };
93
+ this.recovery = {
94
+ canRetry: false,
95
+ suggestions: [],
96
+ ...recovery,
97
+ };
98
+ // Maintain proper stack trace for where our error was thrown
99
+ if (Error.captureStackTrace) {
100
+ Error.captureStackTrace(this, this.constructor);
101
+ }
102
+ }
103
+ /**
104
+ * Adds operational context to this exception for better diagnostics.
105
+ *
106
+ * @param operation - Name of the operation that failed
107
+ * @param context - Additional context key-value pairs
108
+ * @returns This exception for method chaining
109
+ *
110
+ * @example
111
+ * throw new FileNotFound('Not found')
112
+ * .withContext('openFile', { path: '/tmp/doc.pdf' });
113
+ */
114
+ withContext(operation, context = {}) {
115
+ this.details.operation = operation;
116
+ this.details.context = { ...this.details.context, ...context };
117
+ return this;
118
+ }
119
+ /**
120
+ * Get a comprehensive error message with all details
121
+ * @returns Formatted error message with context and suggestions
122
+ */
123
+ getFullMessage() {
124
+ const parts = [
125
+ `[${this.code}] ${this.message}`,
126
+ `Category: ${this.category} | Severity: ${this.severity}`,
127
+ ];
128
+ if (this.details.operation) {
129
+ parts.push(`Operation: ${this.details.operation}`);
130
+ }
131
+ if (this.details.context && Object.keys(this.details.context).length > 0) {
132
+ const contextStr = Object.entries(this.details.context)
133
+ .map(([key, value]) => ` ${key}: ${JSON.stringify(value)}`)
134
+ .join('\n');
135
+ parts.push(`Context:\n${contextStr}`);
136
+ }
137
+ if (this.recovery.suggestions.length > 0) {
138
+ const suggestionsStr = this.recovery.suggestions
139
+ .map((s) => ` • ${s}`)
140
+ .join('\n');
141
+ parts.push(`Recovery Suggestions:\n${suggestionsStr}`);
142
+ }
143
+ if (this.recovery.alternativeApproach) {
144
+ parts.push(`Alternative Approach: ${this.recovery.alternativeApproach}`);
145
+ }
146
+ if (this.recovery.canRetry) {
147
+ const retryMsg = this.recovery.retryAfterMs
148
+ ? `retry after ${this.recovery.retryAfterMs}ms`
149
+ : 'retry';
150
+ parts.push(`Status: Retryable (${retryMsg})`);
151
+ }
152
+ return parts.join('\n');
153
+ }
154
+ /**
155
+ * Convert error to JSON for logging/monitoring
156
+ * @returns JSON representation of the error
157
+ */
158
+ toJSON() {
159
+ return {
160
+ name: this.name,
161
+ code: this.code,
162
+ message: this.message,
163
+ category: this.category,
164
+ severity: this.severity,
165
+ details: this.details,
166
+ recovery: this.recovery,
167
+ stack: this.stack,
168
+ };
169
+ }
170
+ }
171
+ // ===== Parse Errors (1000-1999) =====
172
+ export class ParseException extends PdfException {
173
+ constructor(message, details = {}) {
174
+ super('1000', message, ErrorCategory.PARSING, ErrorSeverity.HIGH, details, {
175
+ canRetry: false,
176
+ suggestions: [
177
+ 'Verify the PDF file is not corrupted',
178
+ 'Try opening the file with Adobe Reader',
179
+ 'Check that the file is a valid PDF',
180
+ ],
181
+ });
182
+ }
183
+ }
184
+ export class InvalidStructure extends PdfException {
185
+ constructor(message, details = {}) {
186
+ super('1101', message, ErrorCategory.PARSING, ErrorSeverity.HIGH, details, {
187
+ canRetry: false,
188
+ suggestions: [
189
+ 'The PDF structure is invalid or incomplete',
190
+ 'Try re-saving the PDF with a PDF tool',
191
+ 'Contact the PDF creator for a corrected version',
192
+ ],
193
+ });
194
+ }
195
+ }
196
+ export class CorruptedData extends PdfException {
197
+ constructor(message, details = {}) {
198
+ super('1200', message, ErrorCategory.PARSING, ErrorSeverity.HIGH, details, {
199
+ canRetry: false,
200
+ suggestions: [
201
+ 'PDF file may be corrupted or damaged',
202
+ 'Try opening the file with Adobe Reader',
203
+ 'Attempt to recover the file using a PDF recovery tool',
204
+ ],
205
+ });
206
+ }
207
+ }
208
+ export class UnsupportedVersion extends PdfException {
209
+ constructor(message, details = {}) {
210
+ super('1300', message, ErrorCategory.UNSUPPORTED, ErrorSeverity.MEDIUM, details, {
211
+ canRetry: false,
212
+ suggestions: [
213
+ 'This PDF version is not supported',
214
+ 'Try converting the PDF to a standard format',
215
+ 'Update your PDF library to a newer version',
216
+ ],
217
+ alternativeApproach: 'Use a PDF converter tool to convert to standard PDF format',
218
+ });
219
+ }
220
+ }
221
+ // ===== I/O Errors (2000-2999) =====
222
+ export class IoException extends PdfException {
223
+ constructor(message, details = {}) {
224
+ super('2000', message, ErrorCategory.IO, ErrorSeverity.HIGH, details, {
225
+ canRetry: true,
226
+ retryAfterMs: 1000,
227
+ suggestions: [
228
+ 'Check file permissions (readable)',
229
+ 'Verify file path exists',
230
+ 'Ensure disk space available',
231
+ 'Check file is not locked by another process',
232
+ ],
233
+ });
234
+ }
235
+ }
236
+ export class FileNotFound extends PdfException {
237
+ constructor(message, details = {}) {
238
+ super('2100', message, ErrorCategory.IO, ErrorSeverity.MEDIUM, details, {
239
+ canRetry: false,
240
+ suggestions: [
241
+ 'Verify the file path is correct',
242
+ 'Check that the file exists',
243
+ 'Ensure the path is absolute or properly resolved',
244
+ ],
245
+ });
246
+ }
247
+ }
248
+ export class PermissionDenied extends PdfException {
249
+ constructor(message, details = {}) {
250
+ super('2200', message, ErrorCategory.PERMISSION, ErrorSeverity.HIGH, details, {
251
+ canRetry: false,
252
+ suggestions: [
253
+ 'Check file permissions using chmod/ls -l',
254
+ 'Ensure the process has read permissions',
255
+ 'Try running with appropriate permissions',
256
+ ],
257
+ alternativeApproach: 'Copy the file to a location with proper permissions',
258
+ });
259
+ }
260
+ }
261
+ export class DiskFull extends PdfException {
262
+ constructor(message, details = {}) {
263
+ super('2300', message, ErrorCategory.RESOURCE, ErrorSeverity.CRITICAL, details, {
264
+ canRetry: true,
265
+ retryAfterMs: 2000,
266
+ suggestions: [
267
+ 'Free up disk space',
268
+ 'Delete unnecessary files',
269
+ 'Archive old files to external storage',
270
+ ],
271
+ alternativeApproach: 'Process the PDF on a device with more available storage',
272
+ });
273
+ }
274
+ }
275
+ export class NetworkError extends PdfException {
276
+ constructor(message, details = {}) {
277
+ super('2400', message, ErrorCategory.IO, ErrorSeverity.MEDIUM, details, {
278
+ canRetry: true,
279
+ retryAfterMs: 2000,
280
+ suggestions: [
281
+ 'Check network connectivity',
282
+ 'Verify the remote server is reachable',
283
+ 'Check firewall and proxy settings',
284
+ ],
285
+ });
286
+ }
287
+ }
288
+ // ===== Encryption Errors (3000-3999) =====
289
+ export class EncryptionException extends PdfException {
290
+ constructor(message, details = {}) {
291
+ super('3000', message, ErrorCategory.ENCRYPTION, ErrorSeverity.HIGH, details, {
292
+ canRetry: false,
293
+ suggestions: [
294
+ 'Check if PDF requires password',
295
+ 'Verify encryption credentials',
296
+ 'Try opening PDF in Adobe Reader to verify',
297
+ ],
298
+ });
299
+ }
300
+ }
301
+ export class InvalidPassword extends PdfException {
302
+ constructor(message, details = {}) {
303
+ super('3100', message, ErrorCategory.ENCRYPTION, ErrorSeverity.MEDIUM, details, {
304
+ canRetry: true,
305
+ retryAfterMs: 1000,
306
+ suggestions: [
307
+ 'Verify the password is correct',
308
+ 'Check for leading/trailing spaces',
309
+ 'Ensure correct keyboard layout is selected',
310
+ 'Try password with different case',
311
+ ],
312
+ });
313
+ }
314
+ }
315
+ export class DecryptionFailed extends PdfException {
316
+ constructor(message, details = {}) {
317
+ super('3200', message, ErrorCategory.ENCRYPTION, ErrorSeverity.HIGH, details, {
318
+ canRetry: false,
319
+ suggestions: [
320
+ 'File may be corrupted',
321
+ 'Encryption method may be unsupported',
322
+ 'Try opening with different PDF reader',
323
+ ],
324
+ });
325
+ }
326
+ }
327
+ export class UnsupportedAlgorithm extends PdfException {
328
+ constructor(message, details = {}) {
329
+ super('3300', message, ErrorCategory.ENCRYPTION, ErrorSeverity.MEDIUM, details, {
330
+ canRetry: false,
331
+ suggestions: [
332
+ 'This encryption algorithm is not supported',
333
+ 'Convert PDF with different encryption algorithm',
334
+ 'Contact PDF creator for unencrypted version',
335
+ ],
336
+ alternativeApproach: 'Use Adobe Acrobat to re-save with supported encryption',
337
+ });
338
+ }
339
+ }
340
+ // ===== State Errors (4000-4999) =====
341
+ export class InvalidStateException extends PdfException {
342
+ constructor(message, details = {}) {
343
+ super('4000', message, ErrorCategory.STATE, ErrorSeverity.HIGH, details, {
344
+ canRetry: false,
345
+ suggestions: [
346
+ 'Ensure the document is in the correct state for this operation',
347
+ 'Check that required initialization steps were completed',
348
+ 'Verify operation order and dependencies',
349
+ ],
350
+ });
351
+ }
352
+ }
353
+ export class DocumentClosed extends PdfException {
354
+ constructor(message, details = {}) {
355
+ super('4100', message, ErrorCategory.STATE, ErrorSeverity.HIGH, details, {
356
+ canRetry: false,
357
+ suggestions: [
358
+ 'The document has been closed and cannot be used',
359
+ 'Create a new document instance or reopen the file',
360
+ 'Check that the document is not being used after closure',
361
+ ],
362
+ });
363
+ }
364
+ }
365
+ export class OperationNotAllowed extends PdfException {
366
+ constructor(message, details = {}) {
367
+ super('4200', message, ErrorCategory.STATE, ErrorSeverity.MEDIUM, details, {
368
+ canRetry: false,
369
+ suggestions: [
370
+ 'This operation is not allowed in the current document state',
371
+ 'Check document permissions and security settings',
372
+ 'Ensure document is writable (not read-only)',
373
+ ],
374
+ });
375
+ }
376
+ }
377
+ export class InvalidOperation extends PdfException {
378
+ constructor(message, details = {}) {
379
+ super('4300', message, ErrorCategory.STATE, ErrorSeverity.MEDIUM, details, {
380
+ canRetry: false,
381
+ suggestions: [
382
+ 'This operation cannot be performed on this document',
383
+ 'Verify the document format and content type',
384
+ 'Check operation prerequisites are met',
385
+ ],
386
+ });
387
+ }
388
+ }
389
+ // ===== Unsupported Feature Errors (5000-5999) =====
390
+ export class UnsupportedFeatureException extends PdfException {
391
+ constructor(message, details = {}) {
392
+ super('5000', message, ErrorCategory.UNSUPPORTED, ErrorSeverity.MEDIUM, details, {
393
+ canRetry: false,
394
+ suggestions: [
395
+ 'This PDF feature is not supported',
396
+ 'Check the documentation for supported features',
397
+ 'Consider using a different PDF tool for this operation',
398
+ ],
399
+ });
400
+ }
401
+ }
402
+ export class FeatureNotImplemented extends PdfException {
403
+ constructor(message, details = {}) {
404
+ super('5100', message, ErrorCategory.UNSUPPORTED, ErrorSeverity.LOW, details, {
405
+ canRetry: false,
406
+ suggestions: [
407
+ 'This feature is not yet implemented',
408
+ 'Check the library changelog for planned features',
409
+ 'File a feature request if this is important for your use case',
410
+ ],
411
+ });
412
+ }
413
+ }
414
+ export class FormatNotSupported extends PdfException {
415
+ constructor(message, details = {}) {
416
+ super('5200', message, ErrorCategory.UNSUPPORTED, ErrorSeverity.MEDIUM, details, {
417
+ canRetry: false,
418
+ suggestions: [
419
+ 'The document format is not supported',
420
+ 'Try converting to a standard format first',
421
+ 'Check documentation for supported formats',
422
+ ],
423
+ alternativeApproach: 'Convert the document using a format converter',
424
+ });
425
+ }
426
+ }
427
+ export class EncodingNotSupported extends PdfException {
428
+ constructor(message, details = {}) {
429
+ super('5300', message, ErrorCategory.UNSUPPORTED, ErrorSeverity.MEDIUM, details, {
430
+ canRetry: false,
431
+ suggestions: [
432
+ 'The character encoding is not supported',
433
+ 'Try using a different character encoding',
434
+ 'Check that the PDF text is properly encoded',
435
+ ],
436
+ });
437
+ }
438
+ }
439
+ // ===== Validation Errors (6000-6999) =====
440
+ export class ValidationException extends PdfException {
441
+ constructor(message, details = {}) {
442
+ super('6000', message, ErrorCategory.VALIDATION, ErrorSeverity.MEDIUM, details, {
443
+ canRetry: false,
444
+ suggestions: [
445
+ 'Verify all parameters are valid and properly formatted',
446
+ 'Check the API documentation for parameter requirements',
447
+ 'Ensure all required fields are provided',
448
+ ],
449
+ });
450
+ }
451
+ }
452
+ export class InvalidParameter extends PdfException {
453
+ constructor(message, details = {}) {
454
+ super('6100', message, ErrorCategory.VALIDATION, ErrorSeverity.MEDIUM, details, {
455
+ canRetry: false,
456
+ suggestions: [
457
+ 'Check that all parameters are valid',
458
+ 'Review the function signature and parameter types',
459
+ 'Ensure parameter values are within valid ranges',
460
+ ],
461
+ });
462
+ }
463
+ }
464
+ export class InvalidValue extends PdfException {
465
+ constructor(message, details = {}) {
466
+ super('6200', message, ErrorCategory.VALIDATION, ErrorSeverity.MEDIUM, details, {
467
+ canRetry: false,
468
+ suggestions: [
469
+ 'Verify the value is correct and properly formatted',
470
+ 'Check acceptable value ranges in the documentation',
471
+ 'Ensure the value type matches the expected type',
472
+ ],
473
+ });
474
+ }
475
+ }
476
+ export class MissingRequired extends PdfException {
477
+ constructor(message, details = {}) {
478
+ super('6300', message, ErrorCategory.VALIDATION, ErrorSeverity.MEDIUM, details, {
479
+ canRetry: false,
480
+ suggestions: [
481
+ 'Provide the required parameter or field',
482
+ 'Check the API documentation for required fields',
483
+ 'Ensure no required parameters are omitted',
484
+ ],
485
+ });
486
+ }
487
+ }
488
+ export class TypeMismatch extends PdfException {
489
+ constructor(message, details = {}) {
490
+ super('6400', message, ErrorCategory.VALIDATION, ErrorSeverity.MEDIUM, details, {
491
+ canRetry: false,
492
+ suggestions: [
493
+ 'Verify the parameter type matches the expected type',
494
+ 'Convert the value to the correct type',
495
+ 'Check the function signature in the documentation',
496
+ ],
497
+ });
498
+ }
499
+ }
500
+ // ===== Rendering Errors (7000-7999) =====
501
+ export class RenderingException extends PdfException {
502
+ constructor(message, details = {}) {
503
+ super('7000', message, ErrorCategory.RENDERING, ErrorSeverity.HIGH, details, {
504
+ canRetry: true,
505
+ retryAfterMs: 1000,
506
+ suggestions: [
507
+ 'Check that the PDF content is valid',
508
+ 'Verify rendering parameters are correct',
509
+ 'Try rendering a different page to isolate the issue',
510
+ ],
511
+ });
512
+ }
513
+ }
514
+ export class RenderFailed extends PdfException {
515
+ constructor(message, details = {}) {
516
+ super('7100', message, ErrorCategory.RENDERING, ErrorSeverity.HIGH, details, {
517
+ canRetry: true,
518
+ retryAfterMs: 1000,
519
+ suggestions: [
520
+ 'The rendering operation failed',
521
+ 'Check available system resources',
522
+ 'Try rendering with lower quality settings',
523
+ ],
524
+ });
525
+ }
526
+ }
527
+ export class UnsupportedRenderFormat extends PdfException {
528
+ constructor(message, details = {}) {
529
+ super('7200', message, ErrorCategory.RENDERING, ErrorSeverity.MEDIUM, details, {
530
+ canRetry: false,
531
+ suggestions: [
532
+ 'The render format is not supported',
533
+ 'Use a supported format (PNG, JPG, PDF, SVG)',
534
+ 'Check documentation for supported rendering formats',
535
+ ],
536
+ });
537
+ }
538
+ }
539
+ export class InsufficientMemory extends PdfException {
540
+ constructor(message, details = {}) {
541
+ super('7300', message, ErrorCategory.RESOURCE, ErrorSeverity.CRITICAL, details, {
542
+ canRetry: true,
543
+ retryAfterMs: 2000,
544
+ suggestions: [
545
+ 'Free up system memory',
546
+ 'Close other applications to reduce memory usage',
547
+ 'Reduce render resolution or quality',
548
+ ],
549
+ alternativeApproach: 'Process the document on a system with more available memory',
550
+ });
551
+ }
552
+ }
553
+ // ===== Search Errors (8000-8999) =====
554
+ export class SearchException extends PdfException {
555
+ constructor(message, details = {}) {
556
+ super('8000', message, ErrorCategory.SEARCH, ErrorSeverity.MEDIUM, details, {
557
+ canRetry: true,
558
+ retryAfterMs: 500,
559
+ suggestions: [
560
+ 'Verify the search term is valid',
561
+ 'Check that the document contains searchable text',
562
+ 'Try a simpler search pattern',
563
+ ],
564
+ });
565
+ }
566
+ }
567
+ export class SearchFailed extends PdfException {
568
+ constructor(message, details = {}) {
569
+ super('8100', message, ErrorCategory.SEARCH, ErrorSeverity.MEDIUM, details, {
570
+ canRetry: true,
571
+ retryAfterMs: 500,
572
+ suggestions: [
573
+ 'The search operation failed',
574
+ 'Verify the document is not corrupted',
575
+ 'Check that the search parameters are valid',
576
+ ],
577
+ });
578
+ }
579
+ }
580
+ export class InvalidPattern extends PdfException {
581
+ constructor(message, details = {}) {
582
+ super('8200', message, ErrorCategory.VALIDATION, ErrorSeverity.MEDIUM, details, {
583
+ canRetry: false,
584
+ suggestions: [
585
+ 'The search pattern is invalid',
586
+ 'Check the regular expression syntax',
587
+ 'Escape special characters if needed',
588
+ ],
589
+ });
590
+ }
591
+ }
592
+ export class IndexCorrupted extends PdfException {
593
+ constructor(message, details = {}) {
594
+ super('8300', message, ErrorCategory.SEARCH, ErrorSeverity.HIGH, details, {
595
+ canRetry: true,
596
+ retryAfterMs: 1000,
597
+ suggestions: [
598
+ 'The search index may be corrupted',
599
+ 'Try rebuilding the search index',
600
+ 'Verify the PDF document is valid',
601
+ ],
602
+ });
603
+ }
604
+ }
605
+ // ===== Signature Errors (8500-8599) =====
606
+ export class SignatureException extends PdfException {
607
+ constructor(message, details = {}) {
608
+ super('8500', message, ErrorCategory.SIGNATURE, ErrorSeverity.HIGH, details, {
609
+ canRetry: false,
610
+ suggestions: [
611
+ 'Verify the certificate file path and password are correct',
612
+ 'Ensure the certificate contains a valid private key for signing',
613
+ 'Check that the PDF data is valid and not corrupted',
614
+ 'Confirm the signing algorithm is supported',
615
+ ],
616
+ });
617
+ }
618
+ }
619
+ export class CertificateLoadFailed extends PdfException {
620
+ constructor(message, details = {}) {
621
+ super('8501', message, ErrorCategory.SIGNATURE, ErrorSeverity.HIGH, details, {
622
+ canRetry: false,
623
+ suggestions: [
624
+ 'Verify the certificate file exists and is readable',
625
+ 'Check the password for PKCS#12 files',
626
+ 'Ensure the PEM files are properly formatted',
627
+ 'Confirm the certificate and key match',
628
+ ],
629
+ });
630
+ }
631
+ }
632
+ export class SigningFailed extends PdfException {
633
+ constructor(message, details = {}) {
634
+ super('8502', message, ErrorCategory.SIGNATURE, ErrorSeverity.HIGH, details, {
635
+ canRetry: true,
636
+ retryAfterMs: 1000,
637
+ suggestions: [
638
+ 'Verify the credentials have a valid private key',
639
+ 'Check that the PDF data is not corrupted',
640
+ 'Ensure the signing algorithm is compatible with the certificate',
641
+ 'Try a different signature subfilter',
642
+ ],
643
+ });
644
+ }
645
+ }
646
+ // ===== Redaction Errors (8600-8699) =====
647
+ export class RedactionException extends PdfException {
648
+ constructor(message, details = {}) {
649
+ super('8600', message, ErrorCategory.REDACTION, ErrorSeverity.HIGH, details, {
650
+ canRetry: false,
651
+ suggestions: [
652
+ 'Verify the redaction area coordinates are valid',
653
+ 'Ensure the document is opened for editing',
654
+ 'Check that the page index is within range',
655
+ 'Confirm the document is not read-only or encrypted',
656
+ ],
657
+ });
658
+ }
659
+ }
660
+ // ===== Accessibility Errors (9500-9599) =====
661
+ export class AccessibilityException extends PdfException {
662
+ constructor(message, details = {}) {
663
+ super('9500', message, ErrorCategory.VALIDATION, ErrorSeverity.HIGH, details, {
664
+ canRetry: false,
665
+ suggestions: [
666
+ 'Check the document structure and tagging',
667
+ 'Verify alt text is set for images and figures',
668
+ 'Ensure document language and title are specified',
669
+ 'Run accessibility validation to identify issues',
670
+ ],
671
+ });
672
+ }
673
+ }
674
+ // ===== Optimization Errors (9600-9699) =====
675
+ export class OptimizationException extends PdfException {
676
+ constructor(message, details = {}) {
677
+ super('9600', message, ErrorCategory.RESOURCE, ErrorSeverity.MEDIUM, details, {
678
+ canRetry: true,
679
+ retryAfterMs: 1000,
680
+ suggestions: [
681
+ 'Check that the document is not corrupted',
682
+ 'Verify sufficient disk space for optimization',
683
+ 'Try optimizing with different settings (DPI, quality)',
684
+ 'Ensure document is not encrypted or read-only',
685
+ ],
686
+ });
687
+ }
688
+ }
689
+ // ===== Compliance Errors (9000-9100) =====
690
+ export class ComplianceException extends PdfException {
691
+ constructor(message, details = {}) {
692
+ super('9000', message, ErrorCategory.COMPLIANCE, ErrorSeverity.HIGH, details, {
693
+ canRetry: false,
694
+ suggestions: [
695
+ 'Check PDF compliance level requirements',
696
+ 'Verify document meets compliance standards',
697
+ 'Review compliance validation report',
698
+ ],
699
+ });
700
+ }
701
+ }
702
+ export class InvalidCompliance extends PdfException {
703
+ constructor(message, details = {}) {
704
+ super('9100', message, ErrorCategory.COMPLIANCE, ErrorSeverity.HIGH, details, {
705
+ canRetry: false,
706
+ suggestions: [
707
+ 'The document does not meet compliance requirements',
708
+ 'Fix the compliance issues identified',
709
+ 'Convert the document to a compliant format',
710
+ ],
711
+ });
712
+ }
713
+ }
714
+ export class ValidationFailed extends PdfException {
715
+ constructor(message, details = {}) {
716
+ super('9150', message, ErrorCategory.COMPLIANCE, ErrorSeverity.MEDIUM, details, {
717
+ canRetry: true,
718
+ retryAfterMs: 1000,
719
+ suggestions: [
720
+ 'Validation failed for this document',
721
+ 'Check the validation error details',
722
+ 'Fix the identified issues and retry',
723
+ ],
724
+ });
725
+ }
726
+ }
727
+ // ===== OCR Errors (9200-9300) =====
728
+ export class OcrException extends PdfException {
729
+ constructor(message, details = {}) {
730
+ super('9200', message, ErrorCategory.OCR, ErrorSeverity.MEDIUM, details, {
731
+ canRetry: true,
732
+ retryAfterMs: 2000,
733
+ suggestions: [
734
+ 'OCR processing failed',
735
+ 'Verify the image quality is sufficient',
736
+ 'Check that the document language is supported',
737
+ ],
738
+ });
739
+ }
740
+ }
741
+ export class RecognitionFailed extends PdfException {
742
+ constructor(message, details = {}) {
743
+ super('9201', message, ErrorCategory.OCR, ErrorSeverity.MEDIUM, details, {
744
+ canRetry: true,
745
+ retryAfterMs: 2000,
746
+ suggestions: [
747
+ 'Text recognition failed',
748
+ 'Try improving the image quality',
749
+ 'Ensure the document language is set correctly',
750
+ ],
751
+ });
752
+ }
753
+ }
754
+ export class LanguageNotSupported extends PdfException {
755
+ constructor(message, details = {}) {
756
+ super('9202', message, ErrorCategory.OCR, ErrorSeverity.MEDIUM, details, {
757
+ canRetry: false,
758
+ suggestions: [
759
+ 'The document language is not supported for OCR',
760
+ 'Check available language packs',
761
+ 'Install language support if available',
762
+ ],
763
+ });
764
+ }
765
+ }
766
+ export class ImageProcessingFailed extends PdfException {
767
+ constructor(message, details = {}) {
768
+ super('9203', message, ErrorCategory.OCR, ErrorSeverity.MEDIUM, details, {
769
+ canRetry: true,
770
+ retryAfterMs: 2000,
771
+ suggestions: [
772
+ 'Image processing failed',
773
+ 'Verify image format and encoding',
774
+ 'Try with a higher quality image',
775
+ ],
776
+ });
777
+ }
778
+ }
779
+ // ===== Other Errors (9900-9999) =====
780
+ export class UnknownError extends PdfException {
781
+ constructor(message, details = {}) {
782
+ super('9900', message, ErrorCategory.UNKNOWN, ErrorSeverity.HIGH, details, {
783
+ canRetry: true,
784
+ retryAfterMs: 1000,
785
+ suggestions: [
786
+ 'An unknown error occurred',
787
+ 'Check the error message for details',
788
+ 'Try the operation again',
789
+ 'File a bug report if the problem persists',
790
+ ],
791
+ });
792
+ }
793
+ }
794
+ export class InternalError extends PdfException {
795
+ constructor(message, details = {}) {
796
+ super('9901', message, ErrorCategory.UNKNOWN, ErrorSeverity.CRITICAL, details, {
797
+ canRetry: true,
798
+ retryAfterMs: 2000,
799
+ suggestions: [
800
+ 'An internal error occurred',
801
+ 'Please restart the application',
802
+ 'Check system resources',
803
+ 'File a bug report with error details',
804
+ ],
805
+ });
806
+ }
807
+ }
808
+ // ===== Error Mapping from Rust FFI =====
809
+ /**
810
+ * Maps Rust error type names to JavaScript exception types.
811
+ */
812
+ export const ERROR_MAP = {
813
+ // Parse errors
814
+ InvalidStructure,
815
+ CorruptedData,
816
+ UnsupportedVersion,
817
+ // I/O errors
818
+ FileNotFound,
819
+ PermissionDenied,
820
+ DiskFull,
821
+ NetworkError,
822
+ // Encryption errors
823
+ InvalidPassword,
824
+ DecryptionFailed,
825
+ UnsupportedAlgorithm,
826
+ // State errors
827
+ DocumentClosed,
828
+ OperationNotAllowed,
829
+ InvalidOperation,
830
+ // Unsupported features
831
+ FeatureNotImplemented,
832
+ FormatNotSupported,
833
+ EncodingNotSupported,
834
+ // Validation errors
835
+ InvalidParameter,
836
+ InvalidValue,
837
+ MissingRequired,
838
+ TypeMismatch,
839
+ // Rendering errors
840
+ RenderFailed,
841
+ UnsupportedRenderFormat,
842
+ InsufficientMemory,
843
+ // Search errors
844
+ SearchFailed,
845
+ InvalidPattern,
846
+ IndexCorrupted,
847
+ // Signature errors
848
+ SignatureException,
849
+ CertificateLoadFailed,
850
+ SigningFailed,
851
+ // Redaction errors
852
+ RedactionException,
853
+ // Accessibility errors
854
+ AccessibilityException,
855
+ // Optimization errors
856
+ OptimizationException,
857
+ // Other errors
858
+ ComplianceException,
859
+ OcrException,
860
+ UnknownError,
861
+ };
862
+ /**
863
+ * Maps a Rust error type to a JavaScript exception.
864
+ *
865
+ * @param rustErrorType - Rust error type name
866
+ * @param message - Error message
867
+ * @param details - Additional context
868
+ * @returns Appropriate error instance
869
+ *
870
+ * @example
871
+ * try {
872
+ * // Native call
873
+ * } catch (err) {
874
+ * const jsErr = mapError('FileNotFound', 'File does not exist', { path: '/tmp/doc.pdf' });
875
+ * throw jsErr;
876
+ * }
877
+ */
878
+ export function mapError(rustErrorType, message, details = {}) {
879
+ const ErrorClass = ERROR_MAP[rustErrorType] || UnknownError;
880
+ return new ErrorClass(message, details);
881
+ }
882
+ /**
883
+ * Maps a numeric FFI error code from the Rust layer to a JavaScript exception.
884
+ *
885
+ * FFI Error Codes:
886
+ * - 0: Success (no error)
887
+ * - 1: I/O error
888
+ * - 2: Parse error
889
+ * - 3: Encryption error
890
+ * - 4: Invalid state error
891
+ * - 5: Rendering unsupported
892
+ * - 6: OCR unsupported
893
+ * - 7: Invalid argument
894
+ * - 8: Signature error
895
+ * - 100: Internal/generic error
896
+ *
897
+ * @param errorCode - Numeric FFI error code from native layer
898
+ * @param message - Optional error message override
899
+ * @returns Appropriate error instance
900
+ *
901
+ * @example
902
+ * const errorCode = nativeCall();
903
+ * if (errorCode !== 0) {
904
+ * throw mapFfiErrorCode(errorCode, 'Operation failed');
905
+ * }
906
+ */
907
+ export function mapFfiErrorCode(errorCode, message) {
908
+ switch (errorCode) {
909
+ case 0:
910
+ return new UnknownError(message ?? 'Success (no error)');
911
+ case 1:
912
+ return new IoException(message ?? 'I/O error: File not found, permission denied, or read/write failed');
913
+ case 2:
914
+ return new ParseException(message ?? 'Parse error: Invalid PDF structure or content stream');
915
+ case 3:
916
+ return new EncryptionException(message ?? 'Encryption error: Incorrect password or unsupported encryption');
917
+ case 4:
918
+ return new InvalidStateException(message ?? 'Invalid state: Operation not allowed in current document state');
919
+ case 5:
920
+ return new UnsupportedFeatureException(message ?? 'Feature not enabled: Rendering support not compiled in');
921
+ case 6:
922
+ return new UnsupportedFeatureException(message ?? 'Feature not enabled: OCR support not compiled in');
923
+ case 7:
924
+ return new InvalidParameter(message ?? 'Invalid argument: Null pointer or invalid parameter passed');
925
+ case 8:
926
+ return new SignatureException(message ?? 'Signature error: Certificate loading, signing, or verification failed');
927
+ case 9:
928
+ return new RedactionException(message ?? 'Redaction error: Content redaction or metadata scrubbing failed');
929
+ case 10:
930
+ return new ComplianceException(message ?? 'Compliance error: PDF/A, PDF/X, or PDF/UA conversion or validation failed');
931
+ case 11:
932
+ return new AccessibilityException(message ?? 'Accessibility error: Tagging, structure tree, or alt text operation failed');
933
+ case 12:
934
+ return new OptimizationException(message ?? 'Optimization error: Font subsetting, image downsampling, or deduplication failed');
935
+ default:
936
+ return new UnknownError(message ?? `Unknown error (code: ${errorCode})`);
937
+ }
938
+ }
939
+ /**
940
+ * Creates an error with optional context.
941
+ *
942
+ * @param code - 4-digit error code
943
+ * @param message - Error message
944
+ * @param options - Configuration options
945
+ * @returns Created exception
946
+ *
947
+ * @example
948
+ * const err = createError('7100', 'Rendering failed', {
949
+ * operation: 'renderPage',
950
+ * context: { page: 0, format: 'png' }
951
+ * });
952
+ */
953
+ export function createError(code, message, options = {}) {
954
+ // Map code to appropriate error class
955
+ const codeMap = {
956
+ '1101': InvalidStructure,
957
+ '1200': CorruptedData,
958
+ '1300': UnsupportedVersion,
959
+ '2100': FileNotFound,
960
+ '2200': PermissionDenied,
961
+ '2300': DiskFull,
962
+ '2400': NetworkError,
963
+ '3100': InvalidPassword,
964
+ '3200': DecryptionFailed,
965
+ '3300': UnsupportedAlgorithm,
966
+ '4100': DocumentClosed,
967
+ '4200': OperationNotAllowed,
968
+ '4300': InvalidOperation,
969
+ '5100': FeatureNotImplemented,
970
+ '5200': FormatNotSupported,
971
+ '5300': EncodingNotSupported,
972
+ '6100': InvalidParameter,
973
+ '6200': InvalidValue,
974
+ '6300': MissingRequired,
975
+ '6400': TypeMismatch,
976
+ '7100': RenderFailed,
977
+ '7200': UnsupportedRenderFormat,
978
+ '7300': InsufficientMemory,
979
+ '8100': SearchFailed,
980
+ '8200': InvalidPattern,
981
+ '8300': IndexCorrupted,
982
+ '8500': SignatureException,
983
+ '8501': CertificateLoadFailed,
984
+ '8502': SigningFailed,
985
+ '8600': RedactionException,
986
+ '9500': AccessibilityException,
987
+ '9600': OptimizationException,
988
+ '9100': InvalidCompliance,
989
+ '9150': ValidationFailed,
990
+ '9200': OcrException,
991
+ '9201': RecognitionFailed,
992
+ '9202': LanguageNotSupported,
993
+ '9203': ImageProcessingFailed,
994
+ };
995
+ const ErrorClass = codeMap[code];
996
+ const context = options.context || {};
997
+ let err;
998
+ if (ErrorClass) {
999
+ err = new ErrorClass(message, context);
1000
+ }
1001
+ else {
1002
+ err = new PdfException(code, message, ErrorCategory.UNKNOWN, ErrorSeverity.HIGH, context);
1003
+ }
1004
+ if (options.operation) {
1005
+ err.withContext(options.operation, context);
1006
+ }
1007
+ return err;
1008
+ }
1009
+ /**
1010
+ * Maps native error to appropriate PDF exception.
1011
+ *
1012
+ * @param error - The error to wrap
1013
+ * @returns Wrapped exception
1014
+ *
1015
+ * @example
1016
+ * try {
1017
+ * // Native call
1018
+ * } catch (err) {
1019
+ * const wrapped = wrapError(err);
1020
+ * console.log(wrapped.code); // e.g., "2100"
1021
+ * }
1022
+ */
1023
+ export function wrapError(error) {
1024
+ // If already a PdfException, return as-is
1025
+ if (error instanceof PdfException) {
1026
+ return error;
1027
+ }
1028
+ let code = '9900';
1029
+ let message = 'Unknown error occurred';
1030
+ let details = {};
1031
+ // Handle Error objects
1032
+ if (error instanceof Error) {
1033
+ message = error.message;
1034
+ // Try to extract code from message format: [XXXX] message
1035
+ const codeMatch = message.match(/^\[(\d{4})\]/);
1036
+ if (codeMatch && codeMatch[1]) {
1037
+ code = codeMatch[1];
1038
+ message = message.replace(/^\[\d{4}\]\s*/, '');
1039
+ }
1040
+ // Copy additional properties
1041
+ const props = Object.getOwnPropertyNames(error);
1042
+ for (const prop of props) {
1043
+ if (prop !== 'message' && prop !== 'name' && prop !== 'stack') {
1044
+ details[prop] = error[prop];
1045
+ }
1046
+ }
1047
+ }
1048
+ // Handle plain objects
1049
+ else if (error && typeof error === 'object') {
1050
+ const obj = error;
1051
+ if (obj.code) {
1052
+ code = String(obj.code);
1053
+ // Convert string codes to 4-digit if needed
1054
+ if (!/^\d{4}$/.test(code)) {
1055
+ code = '9900';
1056
+ }
1057
+ }
1058
+ if (obj.message && typeof obj.message === 'string') {
1059
+ message = obj.message;
1060
+ }
1061
+ details = obj;
1062
+ }
1063
+ // Handle strings
1064
+ else if (typeof error === 'string') {
1065
+ message = error;
1066
+ }
1067
+ try {
1068
+ return new PdfException(code, message, ErrorCategory.UNKNOWN, ErrorSeverity.HIGH, details);
1069
+ }
1070
+ catch {
1071
+ // If code validation fails, use generic error
1072
+ return new UnknownError(message, details);
1073
+ }
1074
+ }
1075
+ /**
1076
+ * Creates a method wrapper that catches native errors and converts them.
1077
+ *
1078
+ * @param fn - The method to wrap
1079
+ * @param thisArg - The context (this) to bind
1080
+ * @returns Wrapped function with error conversion
1081
+ *
1082
+ * @example
1083
+ * const wrapped = wrapMethod(nativeMethod, this);
1084
+ * const result = wrapped(arg1, arg2); // Throws PdfException on error
1085
+ */
1086
+ export function wrapMethod(fn, thisArg = null) {
1087
+ return function (...args) {
1088
+ try {
1089
+ return fn.apply(thisArg || this, args);
1090
+ }
1091
+ catch (nativeErr) {
1092
+ throw wrapError(nativeErr);
1093
+ }
1094
+ };
1095
+ }
1096
+ /**
1097
+ * Creates an async method wrapper that catches native errors.
1098
+ *
1099
+ * @param fn - The async method to wrap
1100
+ * @param thisArg - The context (this) to bind
1101
+ * @returns Wrapped async function with error conversion
1102
+ *
1103
+ * @example
1104
+ * const wrapped = wrapAsyncMethod(nativeAsyncMethod, this);
1105
+ * const result = await wrapped(arg1, arg2); // Throws PdfException on error
1106
+ */
1107
+ export function wrapAsyncMethod(fn, thisArg = null) {
1108
+ return async function (...args) {
1109
+ try {
1110
+ return await fn.apply(thisArg || this, args);
1111
+ }
1112
+ catch (nativeErr) {
1113
+ throw wrapError(nativeErr);
1114
+ }
1115
+ };
1116
+ }
1117
+ //# sourceMappingURL=errors.js.map