-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.zod.ts
More file actions
268 lines (241 loc) · 9.68 KB
/
errors.zod.ts
File metadata and controls
268 lines (241 loc) · 9.68 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
/**
* Standardized Error Codes Protocol
*
* Implements P0 requirement for ObjectStack kernel.
* Provides consistent, machine-readable error codes across the platform.
*
* Features:
* - Categorized error codes (validation, authentication, authorization, etc.)
* - HTTP status code mapping
* - Localization support
* - Retry guidance
*
* Industry alignment: Google Cloud Errors, AWS Error Codes, Stripe API Errors
*/
// ==========================================
// Error Code Categories
// ==========================================
/**
* Error Category Enum
* High-level categorization of errors
*/
export const ErrorCategory = z.enum([
'validation', // Input validation errors (400)
'authentication', // Authentication failures (401)
'authorization', // Permission denied errors (403)
'not_found', // Resource not found (404)
'conflict', // Resource conflict (409)
'rate_limit', // Rate limiting (429)
'server', // Internal server errors (500)
'external', // External service errors (502/503)
'maintenance', // Planned maintenance (503)
]);
export type ErrorCategory = z.infer<typeof ErrorCategory>;
// ==========================================
// Standard Error Codes
// ==========================================
/**
* Standard Error Code Enum
* Machine-readable error codes for common error scenarios
*/
export const StandardErrorCode = z.enum([
// Validation Errors (400)
'validation_error', // Generic validation failure
'invalid_field', // Invalid field value
'missing_required_field', // Required field missing
'invalid_format', // Field format invalid (e.g., email, date)
'value_too_long', // Field value exceeds max length
'value_too_short', // Field value below min length
'value_out_of_range', // Numeric value out of range
'invalid_reference', // Invalid foreign key reference
'duplicate_value', // Unique constraint violation
'invalid_query', // Malformed query syntax
'invalid_filter', // Invalid filter expression
'invalid_sort', // Invalid sort specification
'max_records_exceeded', // Query would return too many records
// Authentication Errors (401)
'unauthenticated', // No valid authentication provided
'invalid_credentials', // Wrong username/password
'expired_token', // Authentication token expired
'invalid_token', // Authentication token invalid
'session_expired', // User session expired
'mfa_required', // Multi-factor authentication required
'email_not_verified', // Email verification required
// Authorization Errors (403)
'permission_denied', // User lacks required permission
'insufficient_privileges', // Operation requires higher privileges
'field_not_accessible', // Field-level security restriction
'record_not_accessible', // Sharing rule restriction
'license_required', // Feature requires license
'ip_restricted', // IP address not allowed
'time_restricted', // Access outside allowed time window
// Not Found Errors (404)
'resource_not_found', // Generic resource not found
'object_not_found', // Object/table not found
'record_not_found', // Record with given ID not found
'field_not_found', // Field not found in object
'endpoint_not_found', // API endpoint not found
// Conflict Errors (409)
'resource_conflict', // Generic resource conflict
'concurrent_modification', // Record modified by another user
'delete_restricted', // Cannot delete due to dependencies
'duplicate_record', // Record already exists
'lock_conflict', // Record is locked by another process
// Rate Limiting (429)
'rate_limit_exceeded', // Too many requests
'quota_exceeded', // API quota exceeded
'concurrent_limit_exceeded', // Too many concurrent requests
// Server Errors (500)
'internal_error', // Generic internal server error
'database_error', // Database operation failed
'timeout', // Operation timed out
'service_unavailable', // Service temporarily unavailable
'not_implemented', // Feature not yet implemented
// External Service Errors (502/503)
'external_service_error', // External API call failed
'integration_error', // Integration service error
'webhook_delivery_failed', // Webhook delivery failed
// Batch Operation Errors
'batch_partial_failure', // Batch operation partially succeeded
'batch_complete_failure', // Batch operation completely failed
'transaction_failed', // Transaction rolled back
]);
export type StandardErrorCode = z.infer<typeof StandardErrorCode>;
// ==========================================
// Enhanced Error Schema
// ==========================================
/**
* HTTP Status Code mapping for error categories
*/
export const ErrorHttpStatusMap: Record<string, number> = {
validation: 400,
authentication: 401,
authorization: 403,
not_found: 404,
conflict: 409,
rate_limit: 429,
server: 500,
external: 502,
maintenance: 503,
};
/**
* Retry Strategy Enum
* Guidance on whether to retry failed requests
*/
export const RetryStrategy = z.enum([
'no_retry', // Do not retry (permanent failure)
'retry_immediate', // Retry immediately
'retry_backoff', // Retry with exponential backoff
'retry_after', // Retry after specified delay
]);
export type RetryStrategy = z.infer<typeof RetryStrategy>;
/**
* Field Error Schema
* Detailed error for a specific field
*/
export const FieldErrorSchema = z.object({
field: z.string().describe('Field path (supports dot notation)'),
code: StandardErrorCode.describe('Error code for this field'),
message: z.string().describe('Human-readable error message'),
value: z.unknown().optional().describe('The invalid value that was provided'),
constraint: z.unknown().optional().describe('The constraint that was violated (e.g., max length)'),
});
export type FieldError = z.infer<typeof FieldErrorSchema>;
/**
* Enhanced API Error Schema
* Standardized error response with detailed metadata
*
* @example Validation Error
* {
* "code": "validation_error",
* "message": "Validation failed for 2 fields",
* "category": "validation",
* "httpStatus": 400,
* "retryable": false,
* "retryStrategy": "no_retry",
* "details": {
* "fieldErrors": [
* {
* "field": "email",
* "code": "invalid_format",
* "message": "Email format is invalid",
* "value": "not-an-email"
* },
* {
* "field": "age",
* "code": "value_out_of_range",
* "message": "Age must be between 0 and 120",
* "value": 150,
* "constraint": { "min": 0, "max": 120 }
* }
* ]
* },
* "timestamp": "2026-01-29T12:00:00Z",
* "requestId": "req_123456",
* "documentation": "https://docs.objectstack.dev/errors/validation_error"
* }
*
* @example Rate Limit Error
* {
* "code": "rate_limit_exceeded",
* "message": "Rate limit exceeded. Try again in 60 seconds.",
* "category": "rate_limit",
* "httpStatus": 429,
* "retryable": true,
* "retryStrategy": "retry_after",
* "retryAfter": 60,
* "details": {
* "limit": 1000,
* "remaining": 0,
* "resetAt": "2026-01-29T13:00:00Z"
* }
* }
*/
export const EnhancedApiErrorSchema = z.object({
code: StandardErrorCode.describe('Machine-readable error code'),
message: z.string().describe('Human-readable error message'),
category: ErrorCategory.optional().describe('Error category'),
httpStatus: z.number().optional().describe('HTTP status code'),
retryable: z.boolean().default(false).describe('Whether the request can be retried'),
retryStrategy: RetryStrategy.optional().describe('Recommended retry strategy'),
retryAfter: z.number().optional().describe('Seconds to wait before retrying'),
details: z.unknown().optional().describe('Additional error context'),
fieldErrors: z.array(FieldErrorSchema).optional().describe('Field-specific validation errors'),
timestamp: z.string().datetime().optional().describe('When the error occurred'),
requestId: z.string().optional().describe('Request ID for tracking'),
traceId: z.string().optional().describe('Distributed trace ID'),
documentation: z.string().url().optional().describe('URL to error documentation'),
helpText: z.string().optional().describe('Suggested actions to resolve the error'),
});
export type EnhancedApiError = z.infer<typeof EnhancedApiErrorSchema>;
// ==========================================
// Error Response Schema
// ==========================================
/**
* Standardized Error Response Schema
* Complete error response envelope
*
* @example
* {
* "success": false,
* "error": {
* "code": "permission_denied",
* "message": "You do not have permission to update this record",
* "category": "authorization",
* "httpStatus": 403,
* "retryable": false
* }
* }
*/
export const ErrorResponseSchema = z.object({
success: z.literal(false).describe('Always false for error responses'),
error: EnhancedApiErrorSchema.describe('Error details'),
meta: z.object({
timestamp: z.string().datetime().optional(),
requestId: z.string().optional(),
traceId: z.string().optional(),
}).optional().describe('Response metadata'),
});
export type ErrorResponse = z.infer<typeof ErrorResponseSchema>;