UNPKG

semanticpen

Version:

AI Article Writer & SEO Blog Generator SDK - Professional TypeScript/JavaScript library for automated content creation, AI-powered article generation, and SEO blog writing with SemanticPen

156 lines 5.99 kB
import { ErrorType } from '../types'; export class SemanticPenError extends Error { constructor(message, type, details) { super(message); this.name = this.constructor.name; this.type = type; this.timestamp = new Date(); this.details = details; if (Error.captureStackTrace) { Error.captureStackTrace(this, this.constructor); } } toJSON() { return { name: this.name, message: this.message, type: this.type, timestamp: this.timestamp.toISOString(), details: this.details, stack: this.stack }; } } export class AuthenticationError extends SemanticPenError { constructor(message = 'Authentication failed', details) { super(message, ErrorType.AUTHENTICATION_ERROR, details); } format() { return `Authentication Error: ${this.message}`; } } export class ValidationError extends SemanticPenError { constructor(message, field, value, details) { super(message, ErrorType.VALIDATION_ERROR, details); this.field = field; this.value = value; } format() { if (this.field) { return `Validation Error in field '${this.field}': ${this.message}`; } return `Validation Error: ${this.message}`; } } export class RateLimitError extends SemanticPenError { constructor(message = 'Rate limit exceeded', retryAfter, details) { super(message, ErrorType.RATE_LIMIT_ERROR, details); this.retryAfter = retryAfter; this.dailyLimit = details?.dailyLimit; this.currentUsage = details?.currentUsage; } format() { let formatted = `Rate Limit Error: ${this.message}`; if (this.retryAfter) { formatted += ` (Retry after ${this.retryAfter} seconds)`; } if (this.dailyLimit && this.currentUsage) { formatted += ` (Usage: ${this.currentUsage}/${this.dailyLimit})`; } return formatted; } } export class NetworkError extends SemanticPenError { constructor(message, statusCode, endpoint, details) { super(message, ErrorType.NETWORK_ERROR, details); this.statusCode = statusCode; this.endpoint = endpoint; } format() { let formatted = `Network Error: ${this.message}`; if (this.statusCode) { formatted += ` (Status: ${this.statusCode})`; } if (this.endpoint) { formatted += ` (Endpoint: ${this.endpoint})`; } return formatted; } } export class ApiError extends SemanticPenError { constructor(message, statusCode, endpoint, response, details) { super(message, ErrorType.API_ERROR, details); this.statusCode = statusCode; this.endpoint = endpoint; this.response = response; } format() { return `API Error [${this.statusCode}] at ${this.endpoint}: ${this.message}`; } static fromResponse(response, endpoint, responseData) { const message = responseData?.error || `HTTP ${response.status}: ${response.statusText}`; return new ApiError(message, response.status, endpoint, responseData); } } export class TimeoutError extends SemanticPenError { constructor(message, timeoutMs, operation, details) { super(message, ErrorType.TIMEOUT_ERROR, details); this.timeoutMs = timeoutMs; this.operation = operation; } format() { let formatted = `Timeout Error: ${this.message} (${this.timeoutMs}ms)`; if (this.operation) { formatted += ` during ${this.operation}`; } return formatted; } } export class UnknownError extends SemanticPenError { constructor(message = 'An unknown error occurred', originalError, details) { super(message, ErrorType.UNKNOWN_ERROR, details); this.originalError = originalError; } format() { let formatted = `Unknown Error: ${this.message}`; if (this.originalError) { formatted += ` (Original: ${this.originalError.message})`; } return formatted; } } export class ErrorFactory { static fromHttpResponse(response, endpoint, responseData) { switch (response.status) { case 401: case 403: return new AuthenticationError(responseData?.error || 'Authentication failed', { statusCode: response.status, endpoint }); case 400: return new ValidationError(responseData?.error || 'Invalid request', undefined, undefined, { statusCode: response.status, endpoint }); case 429: return new RateLimitError(responseData?.error || 'Rate limit exceeded', responseData?.retryAfter, { statusCode: response.status, endpoint }); case 404: return new ApiError(responseData?.error || 'Resource not found', response.status, endpoint, responseData); case 500: case 502: case 503: case 504: return new NetworkError(responseData?.error || 'Server error', response.status, endpoint, { responseData }); default: return new ApiError(responseData?.error || `HTTP ${response.status}`, response.status, endpoint, responseData); } } static fromError(error, context) { if (error instanceof SemanticPenError) { return error; } if (error instanceof TypeError) { return new ValidationError(error.message, undefined, undefined, { originalError: error, context }); } if (error.name === 'AbortError' || error.code === 'ECONNABORTED') { return new TimeoutError(error.message || 'Request timed out', 30000, context, { originalError: error }); } return new UnknownError(error.message || 'An unknown error occurred', error, { context }); } } //# sourceMappingURL=index.js.map