Repository navigation
Expand file tree
/
Copy pathbase.ts
More file actions
117 lines (110 loc) · 2.93 KB
/
Copy pathbase.ts
File metadata and controls
117 lines (110 loc) · 2.93 KB
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
import {
ErrorCode,
ErrorDetails,
ErrorContext,
RecoveryStrategy,
} from "./types.js";
export abstract class BaseError extends Error {
public readonly code: ErrorCode;
public readonly statusCode: number;
public readonly context: ErrorContext;
public readonly recovery: RecoveryStrategy;
public readonly userMessage: string;
public readonly technicalDetails?: string;
public readonly timestamp: string;
constructor(details: ErrorDetails) {
super(details.message);
this.name = this.constructor.name;
this.code = details.code;
this.statusCode = details.statusCode;
this.context = details.context;
this.recovery = details.recovery;
this.userMessage = details.userMessage;
this.technicalDetails = details.technicalDetails;
this.timestamp = new Date().toISOString();
// Ensure proper prototype chain
Object.setPrototypeOf(this, new.target.prototype);
}
toJSON() {
return {
error: {
code: this.code,
message: this.userMessage,
statusCode: this.statusCode,
timestamp: this.timestamp,
context: this.context,
recovery: this.recovery,
technicalDetails: this.technicalDetails,
},
};
}
}
// Specific error classes
export class ValidationError extends BaseError {
constructor(
message: string,
context: ErrorContext,
technicalDetails?: string
) {
super({
code: ErrorCode.INVALID_CONFIGURATION,
message,
statusCode: 400,
context,
recovery: {
type: "manual",
description: "Please check your input parameters and try again",
},
userMessage: message,
technicalDetails,
});
}
}
export class AIServiceError extends BaseError {
constructor(
provider: string,
model: string,
originalError: string,
context: ErrorContext
) {
const message = `AI service error from ${provider}:${model}`;
super({
code: ErrorCode.AI_SERVICE_ERROR,
message,
statusCode: 502,
context,
recovery: {
type: "retry",
description: "Retry the request or try a different AI provider",
autoRetry: true,
maxRetries: 3,
fallbackAction: "Switch to alternative AI provider",
},
userMessage: "AI service is temporarily unavailable. Please try again.",
technicalDetails: originalError,
});
}
}
export class ProcessingError extends BaseError {
constructor(
operation: string,
context: ErrorContext,
technicalDetails?: string
) {
super({
code: ErrorCode.PROCESSING_FAILED,
message: `Processing failed during ${operation}`,
statusCode: 500,
context,
recovery: {
type: "retry",
description:
"Retry the operation or contact support if the issue persists",
autoRetry: false,
maxRetries: 1,
},
userMessage: `Failed to process ${operation}. Please try again.`,
technicalDetails,
});
}
}