UNPKG

prisma-zod-generator

Version:

Prisma 2+ generator to emit Zod schemas from your Prisma schema

151 lines 5.15 kB
"use strict"; /** * Standardized error handling utilities for PZG Pro */ Object.defineProperty(exports, "__esModule", { value: true }); exports.SecurityError = exports.FileSystemError = exports.ValidationError = exports.LicenseError = exports.PZGProError = void 0; exports.safeAsync = safeAsync; exports.safeSync = safeSync; exports.withRetry = withRetry; exports.assertDefined = assertDefined; exports.assertNonEmpty = assertNonEmpty; exports.safeFileOperation = safeFileOperation; exports.logError = logError; class PZGProError extends Error { constructor(message, code, feature, context) { super(message); this.code = code; this.feature = feature; this.context = context; this.name = 'PZGProError'; } } exports.PZGProError = PZGProError; class LicenseError extends PZGProError { constructor(message, context) { super(message, 'LICENSE_ERROR', 'license', context); this.name = 'LicenseError'; } } exports.LicenseError = LicenseError; class ValidationError extends PZGProError { constructor(message, feature, context) { super(message, 'VALIDATION_ERROR', feature, context); this.name = 'ValidationError'; } } exports.ValidationError = ValidationError; class FileSystemError extends PZGProError { constructor(message, feature, context) { super(message, 'FILESYSTEM_ERROR', feature, context); this.name = 'FileSystemError'; } } exports.FileSystemError = FileSystemError; class SecurityError extends PZGProError { constructor(message, feature, context) { super(message, 'SECURITY_ERROR', feature, context); this.name = 'SecurityError'; } } exports.SecurityError = SecurityError; /** * Safe async wrapper that handles errors consistently */ async function safeAsync(operation, feature, context) { try { const result = await operation(); return { success: true, data: result }; } catch (error) { if (error instanceof PZGProError) { return { success: false, error }; } const wrappedError = new PZGProError(error instanceof Error ? error.message : 'Unknown error occurred', 'UNKNOWN_ERROR', feature, { ...context, originalError: error }); return { success: false, error: wrappedError }; } } /** * Safe sync wrapper that handles errors consistently */ function safeSync(operation, feature, context) { try { const result = operation(); return { success: true, data: result }; } catch (error) { if (error instanceof PZGProError) { return { success: false, error }; } const wrappedError = new PZGProError(error instanceof Error ? error.message : 'Unknown error occurred', 'UNKNOWN_ERROR', feature, { ...context, originalError: error }); return { success: false, error: wrappedError }; } } /** * Retry wrapper for operations that may fail temporarily */ async function withRetry(operation, maxAttempts = 3, delayMs = 1000, feature = 'unknown') { let lastError; for (let attempt = 1; attempt <= maxAttempts; attempt++) { try { return await operation(); } catch (error) { lastError = error instanceof Error ? error : new Error('Unknown error'); if (attempt === maxAttempts) { throw new PZGProError(`Operation failed after ${maxAttempts} attempts: ${lastError.message}`, 'RETRY_EXHAUSTED', feature, { maxAttempts, originalError: lastError }); } // Wait before retrying await new Promise((resolve) => setTimeout(resolve, delayMs * attempt)); } } throw lastError; } /** * Validate that a value is not null or undefined */ function assertDefined(value, message, feature) { if (value === null || value === undefined) { throw new ValidationError(message, feature, { value }); } } /** * Validate that a string is not empty */ function assertNonEmpty(value, message, feature) { if (!value || value.trim().length === 0) { throw new ValidationError(message, feature, { value }); } } /** * Safe file operation wrapper */ async function safeFileOperation(operation, filePath, feature) { try { return await operation(); } catch (error) { const message = error instanceof Error ? error.message : 'File operation failed'; throw new FileSystemError(`Failed to perform file operation on ${filePath}: ${message}`, feature, { filePath, originalError: error }); } } /** * Production-safe logger that doesn't expose sensitive information */ function logError(error) { // Only log safe information in production const safeContext = { code: error.code, feature: error.feature, message: error.message, timestamp: new Date().toISOString(), }; // In development, include more context if (process.env.NODE_ENV === 'development') { console.error('[PZG Pro Error]', safeContext, error.context); } else { console.error('[PZG Pro Error]', safeContext); } } //# sourceMappingURL=errorHandling.js.map