@ketrics/sdk-backend 0.2.0 → 0.4.0

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.
@@ -0,0 +1,212 @@
1
+ "use strict";
2
+ /**
3
+ * Ketrics SDK - Job Error Classes
4
+ *
5
+ * Provides typed errors for background job operations in tenant applications.
6
+ *
7
+ * Error Hierarchy:
8
+ * - JobError (base)
9
+ * - JobNotFoundError
10
+ * - InvalidFunctionError
11
+ * - CrossAppPermissionError
12
+ * - JobExecutionError
13
+ */
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.JobExecutionError = exports.CrossAppPermissionError = exports.InvalidFunctionError = exports.JobNotFoundError = exports.JobError = void 0;
16
+ exports.isJobError = isJobError;
17
+ exports.isJobErrorType = isJobErrorType;
18
+ // ============================================================================
19
+ // Base Error Class
20
+ // ============================================================================
21
+ /**
22
+ * Base error class for all Job errors
23
+ *
24
+ * All Job errors extend this class and include:
25
+ * - jobId: The job ID that caused the error (if applicable)
26
+ * - operation: The operation that failed
27
+ * - timestamp: When the error occurred
28
+ */
29
+ class JobError extends Error {
30
+ constructor(message, operation, jobId) {
31
+ super(message);
32
+ this.name = this.constructor.name;
33
+ this.operation = operation;
34
+ this.jobId = jobId;
35
+ this.timestamp = new Date();
36
+ // Maintains proper stack trace for where error was thrown
37
+ if (Error.captureStackTrace) {
38
+ Error.captureStackTrace(this, this.constructor);
39
+ }
40
+ }
41
+ /**
42
+ * Serialize error for logging
43
+ */
44
+ toJSON() {
45
+ return {
46
+ name: this.name,
47
+ message: this.message,
48
+ jobId: this.jobId,
49
+ operation: this.operation,
50
+ timestamp: this.timestamp.toISOString(),
51
+ };
52
+ }
53
+ }
54
+ exports.JobError = JobError;
55
+ // ============================================================================
56
+ // Specific Error Classes
57
+ // ============================================================================
58
+ /**
59
+ * Error thrown when a job is not found
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * try {
64
+ * const status = await ketrics.Job.getStatus('invalid-job-id');
65
+ * } catch (error) {
66
+ * if (error instanceof ketrics.JobNotFoundError) {
67
+ * console.log(`Job '${error.jobId}' not found`);
68
+ * }
69
+ * }
70
+ * ```
71
+ */
72
+ class JobNotFoundError extends JobError {
73
+ constructor(jobId) {
74
+ super(`Job '${jobId}' not found. Verify the job ID is correct and the job exists.`, 'getStatus', jobId);
75
+ }
76
+ }
77
+ exports.JobNotFoundError = JobNotFoundError;
78
+ /**
79
+ * Error thrown when function name is invalid or missing
80
+ *
81
+ * @example
82
+ * ```typescript
83
+ * try {
84
+ * await ketrics.Job.runInBackground({ function: '' });
85
+ * } catch (error) {
86
+ * if (error instanceof ketrics.InvalidFunctionError) {
87
+ * console.log(`Invalid function: ${error.functionName}`);
88
+ * }
89
+ * }
90
+ * ```
91
+ */
92
+ class InvalidFunctionError extends JobError {
93
+ constructor(functionName) {
94
+ super(`Invalid function name '${functionName}'. Function name must be a non-empty string.`, 'runInBackground');
95
+ this.functionName = functionName;
96
+ }
97
+ toJSON() {
98
+ return {
99
+ ...super.toJSON(),
100
+ functionName: this.functionName,
101
+ };
102
+ }
103
+ }
104
+ exports.InvalidFunctionError = InvalidFunctionError;
105
+ /**
106
+ * Error thrown when cross-app job lacks required permission
107
+ *
108
+ * This error occurs when attempting to create a job in another application
109
+ * without a valid application grant that includes background job permission.
110
+ *
111
+ * @example
112
+ * ```typescript
113
+ * try {
114
+ * await ketrics.Job.runInBackground({
115
+ * application: 'other-app',
116
+ * function: 'processData',
117
+ * });
118
+ * } catch (error) {
119
+ * if (error instanceof ketrics.CrossAppPermissionError) {
120
+ * console.log(`Cannot create job from '${error.sourceApp}' to '${error.targetApp}'`);
121
+ * }
122
+ * }
123
+ * ```
124
+ */
125
+ class CrossAppPermissionError extends JobError {
126
+ constructor(sourceApp, targetApp) {
127
+ super(`Cross-app job permission denied. Application '${sourceApp}' does not have background job permission for '${targetApp}'.`, 'runInBackground');
128
+ this.sourceApp = sourceApp;
129
+ this.targetApp = targetApp;
130
+ }
131
+ toJSON() {
132
+ return {
133
+ ...super.toJSON(),
134
+ sourceApp: this.sourceApp,
135
+ targetApp: this.targetApp,
136
+ };
137
+ }
138
+ }
139
+ exports.CrossAppPermissionError = CrossAppPermissionError;
140
+ /**
141
+ * Error thrown when job execution fails
142
+ *
143
+ * This error wraps runtime errors that occur during job execution.
144
+ *
145
+ * @example
146
+ * ```typescript
147
+ * const status = await ketrics.Job.getStatus(jobId);
148
+ * if (status.status === 'failed' && status.error) {
149
+ * console.log(`Job failed with code ${status.error.code}: ${status.error.message}`);
150
+ * }
151
+ * ```
152
+ */
153
+ class JobExecutionError extends JobError {
154
+ constructor(jobId, errorCode, message) {
155
+ super(`Job '${jobId}' execution failed: ${message}`, 'execute', jobId);
156
+ this.errorCode = errorCode;
157
+ }
158
+ toJSON() {
159
+ return {
160
+ ...super.toJSON(),
161
+ errorCode: this.errorCode,
162
+ };
163
+ }
164
+ }
165
+ exports.JobExecutionError = JobExecutionError;
166
+ // ============================================================================
167
+ // Type Guards
168
+ // ============================================================================
169
+ /**
170
+ * Type guard to check if an error is a JobError
171
+ *
172
+ * @param error - The error to check
173
+ * @returns True if the error is a JobError
174
+ *
175
+ * @example
176
+ * ```typescript
177
+ * try {
178
+ * await ketrics.Job.runInBackground({ function: 'myFunc' });
179
+ * } catch (error) {
180
+ * if (isJobError(error)) {
181
+ * console.log(`Job error during ${error.operation}: ${error.message}`);
182
+ * }
183
+ * }
184
+ * ```
185
+ */
186
+ function isJobError(error) {
187
+ return error instanceof JobError;
188
+ }
189
+ /**
190
+ * Type guard to check if an error is a specific JobError type
191
+ *
192
+ * @param error - The error to check
193
+ * @param errorClass - The error class to check against
194
+ * @returns True if the error is an instance of the specified class
195
+ *
196
+ * @example
197
+ * ```typescript
198
+ * try {
199
+ * await ketrics.Job.getStatus('invalid-id');
200
+ * } catch (error) {
201
+ * if (isJobErrorType(error, JobNotFoundError)) {
202
+ * console.log('Job not found');
203
+ * } else if (isJobErrorType(error, CrossAppPermissionError)) {
204
+ * console.log('Permission denied');
205
+ * }
206
+ * }
207
+ * ```
208
+ */
209
+ function isJobErrorType(error, errorClass) {
210
+ return error instanceof errorClass;
211
+ }
212
+ //# sourceMappingURL=job-errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"job-errors.js","sourceRoot":"","sources":["../src/job-errors.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;GAWG;;;AAqNH,gCAEC;AAsBD,wCAKC;AAhPD,+EAA+E;AAC/E,mBAAmB;AACnB,+EAA+E;AAE/E;;;;;;;GAOG;AACH,MAAsB,QAAS,SAAQ,KAAK;IAU1C,YAAY,OAAe,EAAE,SAAiB,EAAE,KAAc;QAC5D,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;QAClC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,SAAS,GAAG,IAAI,IAAI,EAAE,CAAC;QAE5B,0DAA0D;QAC1D,IAAI,KAAK,CAAC,iBAAiB,EAAE,CAAC;YAC5B,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;QAClD,CAAC;IACH,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,OAAO;YACL,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,KAAK,EAAE,IAAI,CAAC,KAAK;YACjB,SAAS,EAAE,IAAI,CAAC,SAAS;YACzB,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,WAAW,EAAE;SACxC,CAAC;IACJ,CAAC;CACF;AAnCD,4BAmCC;AAED,+EAA+E;AAC/E,yBAAyB;AACzB,+EAA+E;AAE/E;;;;;;;;;;;;;GAaG;AACH,MAAa,gBAAiB,SAAQ,QAAQ;IAC5C,YAAY,KAAa;QACvB,KAAK,CACH,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,CAAC;CACF;AAnBD,8CAmBC;AAED,+EAA+E;AAC/E,cAAc;AACd,+EAA+E;AAE/E;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,UAAU,CAAC,KAAc;IACvC,OAAO,KAAK,YAAY,QAAQ,CAAC;AACnC,CAAC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,cAAc,CAC5B,KAAc,EACd,UAAuC;IAEvC,OAAO,KAAK,YAAY,UAAU,CAAC;AACrC,CAAC"}
package/dist/job.d.ts ADDED
@@ -0,0 +1,158 @@
1
+ /**
2
+ * Ketrics SDK - Job Interfaces and Types
3
+ *
4
+ * Provides type definitions for background job execution
5
+ * in tenant applications.
6
+ *
7
+ * Usage in tenant code:
8
+ * ```typescript
9
+ * // Queue a background job
10
+ * const jobId = await ketrics.Job.runInBackground({
11
+ * function: 'generateReport',
12
+ * payload: { year: 2024 },
13
+ * options: { timeout: 600000 }
14
+ * });
15
+ *
16
+ * // Check job status
17
+ * const status = await ketrics.Job.getStatus(jobId);
18
+ * if (status.status === 'completed') {
19
+ * console.log('Job completed at:', status.completedAt);
20
+ * }
21
+ *
22
+ * // List pending jobs
23
+ * const result = await ketrics.Job.list({ status: 'pending', limit: 10 });
24
+ * for (const job of result.jobs) {
25
+ * console.log(job.jobId, job.functionName);
26
+ * }
27
+ * ```
28
+ */
29
+ /**
30
+ * Job status string literal type
31
+ */
32
+ export type JobStatusValue = 'pending' | 'running' | 'completed' | 'failed';
33
+ /**
34
+ * Options for background job execution
35
+ */
36
+ export interface RunInBackgroundOptions {
37
+ /**
38
+ * Execution timeout in milliseconds
39
+ *
40
+ * @default 300000 (5 minutes)
41
+ * @max 900000 (15 minutes)
42
+ */
43
+ timeout?: number;
44
+ /**
45
+ * Idempotency key to prevent duplicate job creation
46
+ *
47
+ * If a job with the same idempotency key already exists,
48
+ * the existing job ID will be returned instead of creating a new job.
49
+ */
50
+ idempotencyKey?: string;
51
+ }
52
+ /**
53
+ * Parameters for Job.runInBackground()
54
+ */
55
+ export interface RunInBackgroundParams {
56
+ /**
57
+ * Target application code (optional)
58
+ *
59
+ * If specified, creates a cross-app job that executes in another application.
60
+ * Requires a valid application grant with background job permission.
61
+ * If omitted, the job executes in the current application.
62
+ */
63
+ application?: string;
64
+ /**
65
+ * Function name to execute
66
+ *
67
+ * Must be an exported function in the target application.
68
+ */
69
+ function: string;
70
+ /**
71
+ * Payload to pass to the function
72
+ *
73
+ * This object will be passed as the first argument to the function.
74
+ */
75
+ payload?: Record<string, unknown>;
76
+ /**
77
+ * Execution options
78
+ */
79
+ options?: RunInBackgroundOptions;
80
+ }
81
+ /**
82
+ * Job error details
83
+ */
84
+ export interface JobErrorDetails {
85
+ /**
86
+ * Error code
87
+ *
88
+ * Common codes:
89
+ * - 'TIMEOUT': Job exceeded execution timeout
90
+ * - 'RUNTIME_ERROR': Unhandled exception in function
91
+ * - 'FUNCTION_NOT_FOUND': Target function does not exist
92
+ */
93
+ code: string;
94
+ /** Human-readable error message */
95
+ message: string;
96
+ }
97
+ /**
98
+ * Job status returned from getStatus() and list()
99
+ */
100
+ export interface JobStatus {
101
+ /** Job UUID */
102
+ jobId: string;
103
+ /** Application UUID where the job runs */
104
+ applicationId: string;
105
+ /** Application code where the job runs */
106
+ applicationCode: string;
107
+ /** Function name being executed */
108
+ functionName: string;
109
+ /** Current job status */
110
+ status: JobStatusValue;
111
+ /** ISO timestamp when the job was created */
112
+ createdAt: string;
113
+ /** ISO timestamp when the job started execution (optional) */
114
+ startedAt?: string;
115
+ /** ISO timestamp when the job completed (optional) */
116
+ completedAt?: string;
117
+ /** Error details if the job failed (optional) */
118
+ error?: JobErrorDetails;
119
+ }
120
+ /**
121
+ * Parameters for Job.list()
122
+ */
123
+ export interface JobListParams {
124
+ /**
125
+ * Filter by job status
126
+ *
127
+ * If omitted, returns jobs of all statuses.
128
+ */
129
+ status?: JobStatusValue;
130
+ /**
131
+ * Maximum number of results to return
132
+ *
133
+ * @default 20
134
+ * @max 100
135
+ */
136
+ limit?: number;
137
+ /**
138
+ * Pagination cursor from previous response
139
+ *
140
+ * Pass the `cursor` from a previous JobListResult to fetch the next page.
141
+ */
142
+ cursor?: string;
143
+ }
144
+ /**
145
+ * Result from Job.list()
146
+ */
147
+ export interface JobListResult {
148
+ /** Array of job status objects */
149
+ jobs: JobStatus[];
150
+ /**
151
+ * Pagination cursor for fetching the next page
152
+ *
153
+ * If present, more results are available. Pass this value as
154
+ * the `cursor` parameter in the next call to get the next page.
155
+ */
156
+ cursor?: string;
157
+ }
158
+ //# sourceMappingURL=job.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"job.d.ts","sourceRoot":"","sources":["../src/job.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAMH;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,SAAS,GAAG,SAAS,GAAG,WAAW,GAAG,QAAQ,CAAC;AAM5E;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC;;;;;OAKG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;;;;OAMG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;;;OAIG;IACH,QAAQ,EAAE,MAAM,CAAC;IAEjB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAElC;;OAEG;IACH,OAAO,CAAC,EAAE,sBAAsB,CAAC;CAClC;AAMD;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B;;;;;;;OAOG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb,mCAAmC;IACnC,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,eAAe;IACf,KAAK,EAAE,MAAM,CAAC;IAEd,0CAA0C;IAC1C,aAAa,EAAE,MAAM,CAAC;IAEtB,0CAA0C;IAC1C,eAAe,EAAE,MAAM,CAAC;IAExB,mCAAmC;IACnC,YAAY,EAAE,MAAM,CAAC;IAErB,yBAAyB;IACzB,MAAM,EAAE,cAAc,CAAC;IAEvB,6CAA6C;IAC7C,SAAS,EAAE,MAAM,CAAC;IAElB,8DAA8D;IAC9D,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB,sDAAsD;IACtD,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB,iDAAiD;IACjD,KAAK,CAAC,EAAE,eAAe,CAAC;CACzB;AAMD;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B;;;;OAIG;IACH,MAAM,CAAC,EAAE,cAAc,CAAC;IAExB;;;;;OAKG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,kCAAkC;IAClC,IAAI,EAAE,SAAS,EAAE,CAAC;IAElB;;;;;OAKG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB"}
package/dist/job.js ADDED
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ /**
3
+ * Ketrics SDK - Job Interfaces and Types
4
+ *
5
+ * Provides type definitions for background job execution
6
+ * in tenant applications.
7
+ *
8
+ * Usage in tenant code:
9
+ * ```typescript
10
+ * // Queue a background job
11
+ * const jobId = await ketrics.Job.runInBackground({
12
+ * function: 'generateReport',
13
+ * payload: { year: 2024 },
14
+ * options: { timeout: 600000 }
15
+ * });
16
+ *
17
+ * // Check job status
18
+ * const status = await ketrics.Job.getStatus(jobId);
19
+ * if (status.status === 'completed') {
20
+ * console.log('Job completed at:', status.completedAt);
21
+ * }
22
+ *
23
+ * // List pending jobs
24
+ * const result = await ketrics.Job.list({ status: 'pending', limit: 10 });
25
+ * for (const job of result.jobs) {
26
+ * console.log(job.jobId, job.functionName);
27
+ * }
28
+ * ```
29
+ */
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ //# sourceMappingURL=job.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"job.js","sourceRoot":"","sources":["../src/job.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG"}
@@ -0,0 +1,77 @@
1
+ /**
2
+ * PDF SDK Error Classes
3
+ *
4
+ * Error types for PDF operations in tenant applications.
5
+ */
6
+ /**
7
+ * Base error for all PDF operations
8
+ *
9
+ * All PDF-related errors inherit from this class, providing
10
+ * consistent context and structure for error handling.
11
+ */
12
+ export declare abstract class PdfError extends Error {
13
+ /** Operation that caused the error */
14
+ readonly operation: string;
15
+ /** Timestamp when the error occurred */
16
+ readonly timestamp: Date;
17
+ constructor(operation: string, message: string);
18
+ /**
19
+ * Convert error to JSON-serializable object
20
+ */
21
+ toJSON(): Record<string, unknown>;
22
+ }
23
+ /**
24
+ * Error parsing PDF file
25
+ *
26
+ * Thrown when a PDF file cannot be parsed due to invalid format,
27
+ * corrupted data, or unsupported features.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * try {
32
+ * const doc = await ketrics.Pdf.read(buffer);
33
+ * } catch (error) {
34
+ * if (error instanceof ketrics.PdfParseError) {
35
+ * console.log('Failed to parse PDF file:', error.message);
36
+ * }
37
+ * }
38
+ * ```
39
+ */
40
+ export declare class PdfParseError extends PdfError {
41
+ /** The reason for the parse failure */
42
+ readonly reason: string;
43
+ constructor(reason: string);
44
+ toJSON(): Record<string, unknown>;
45
+ }
46
+ /**
47
+ * Error writing PDF file
48
+ *
49
+ * Thrown when a PDF file cannot be written due to invalid data
50
+ * or other write errors.
51
+ *
52
+ * @example
53
+ * ```typescript
54
+ * try {
55
+ * const buffer = await doc.toBuffer();
56
+ * } catch (error) {
57
+ * if (error instanceof ketrics.PdfWriteError) {
58
+ * console.log('Failed to write PDF file:', error.message);
59
+ * }
60
+ * }
61
+ * ```
62
+ */
63
+ export declare class PdfWriteError extends PdfError {
64
+ /** The reason for the write failure */
65
+ readonly reason: string;
66
+ constructor(reason: string);
67
+ toJSON(): Record<string, unknown>;
68
+ }
69
+ /**
70
+ * Type guard to check if an error is a PdfError
71
+ */
72
+ export declare function isPdfError(error: unknown): error is PdfError;
73
+ /**
74
+ * Type guard to check if an error is a specific PDF error type
75
+ */
76
+ export declare function isPdfErrorType<T extends PdfError>(error: unknown, errorClass: new (...args: never[]) => T): error is T;
77
+ //# sourceMappingURL=pdf-errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pdf-errors.d.ts","sourceRoot":"","sources":["../src/pdf-errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAMH;;;;;GAKG;AACH,8BAAsB,QAAS,SAAQ,KAAK;IAC1C,sCAAsC;IACtC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAE3B,wCAAwC;IACxC,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC;gBAEb,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAc9C;;OAEG;IACH,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAQlC;AAMD;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,aAAc,SAAQ,QAAQ;IACzC,uCAAuC;IACvC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;gBAEZ,MAAM,EAAE,MAAM;IAK1B,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAMlC;AAMD;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,aAAc,SAAQ,QAAQ;IACzC,uCAAuC;IACvC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;gBAEZ,MAAM,EAAE,MAAM;IAK1B,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAMlC;AAMD;;GAEG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,QAAQ,CAE5D;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,CAAC,SAAS,QAAQ,EAC/C,KAAK,EAAE,OAAO,EACd,UAAU,EAAE,KAAK,GAAG,IAAI,EAAE,KAAK,EAAE,KAAK,CAAC,GACtC,KAAK,IAAI,CAAC,CAEZ"}
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ /**
3
+ * PDF SDK Error Classes
4
+ *
5
+ * Error types for PDF operations in tenant applications.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.PdfWriteError = exports.PdfParseError = exports.PdfError = void 0;
9
+ exports.isPdfError = isPdfError;
10
+ exports.isPdfErrorType = isPdfErrorType;
11
+ // ============================================================================
12
+ // Base PDF Error
13
+ // ============================================================================
14
+ /**
15
+ * Base error for all PDF operations
16
+ *
17
+ * All PDF-related errors inherit from this class, providing
18
+ * consistent context and structure for error handling.
19
+ */
20
+ class PdfError extends Error {
21
+ constructor(operation, message) {
22
+ super(message);
23
+ this.name = this.constructor.name;
24
+ this.operation = operation;
25
+ this.timestamp = new Date();
26
+ // Maintain proper stack trace in V8 environments
27
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
28
+ if (Error.captureStackTrace) {
29
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
30
+ Error.captureStackTrace(this, this.constructor);
31
+ }
32
+ }
33
+ /**
34
+ * Convert error to JSON-serializable object
35
+ */
36
+ toJSON() {
37
+ return {
38
+ name: this.name,
39
+ message: this.message,
40
+ operation: this.operation,
41
+ timestamp: this.timestamp.toISOString(),
42
+ };
43
+ }
44
+ }
45
+ exports.PdfError = PdfError;
46
+ // ============================================================================
47
+ // PDF Parse Error
48
+ // ============================================================================
49
+ /**
50
+ * Error parsing PDF file
51
+ *
52
+ * Thrown when a PDF file cannot be parsed due to invalid format,
53
+ * corrupted data, or unsupported features.
54
+ *
55
+ * @example
56
+ * ```typescript
57
+ * try {
58
+ * const doc = await ketrics.Pdf.read(buffer);
59
+ * } catch (error) {
60
+ * if (error instanceof ketrics.PdfParseError) {
61
+ * console.log('Failed to parse PDF file:', error.message);
62
+ * }
63
+ * }
64
+ * ```
65
+ */
66
+ class PdfParseError extends PdfError {
67
+ constructor(reason) {
68
+ super('read', `Failed to parse PDF file: ${reason}`);
69
+ this.reason = reason;
70
+ }
71
+ toJSON() {
72
+ return {
73
+ ...super.toJSON(),
74
+ reason: this.reason,
75
+ };
76
+ }
77
+ }
78
+ exports.PdfParseError = PdfParseError;
79
+ // ============================================================================
80
+ // PDF Write Error
81
+ // ============================================================================
82
+ /**
83
+ * Error writing PDF file
84
+ *
85
+ * Thrown when a PDF file cannot be written due to invalid data
86
+ * or other write errors.
87
+ *
88
+ * @example
89
+ * ```typescript
90
+ * try {
91
+ * const buffer = await doc.toBuffer();
92
+ * } catch (error) {
93
+ * if (error instanceof ketrics.PdfWriteError) {
94
+ * console.log('Failed to write PDF file:', error.message);
95
+ * }
96
+ * }
97
+ * ```
98
+ */
99
+ class PdfWriteError extends PdfError {
100
+ constructor(reason) {
101
+ super('write', `Failed to write PDF file: ${reason}`);
102
+ this.reason = reason;
103
+ }
104
+ toJSON() {
105
+ return {
106
+ ...super.toJSON(),
107
+ reason: this.reason,
108
+ };
109
+ }
110
+ }
111
+ exports.PdfWriteError = PdfWriteError;
112
+ // ============================================================================
113
+ // Type Guards
114
+ // ============================================================================
115
+ /**
116
+ * Type guard to check if an error is a PdfError
117
+ */
118
+ function isPdfError(error) {
119
+ return error instanceof PdfError;
120
+ }
121
+ /**
122
+ * Type guard to check if an error is a specific PDF error type
123
+ */
124
+ function isPdfErrorType(error, errorClass) {
125
+ return error instanceof errorClass;
126
+ }
127
+ //# sourceMappingURL=pdf-errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pdf-errors.js","sourceRoot":"","sources":["../src/pdf-errors.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAiIH,gCAEC;AAKD,wCAKC;AA3ID,+EAA+E;AAC/E,iBAAiB;AACjB,+EAA+E;AAE/E;;;;;G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