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
168 lines • 6.64 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ErrorFactory = exports.UnknownError = exports.TimeoutError = exports.ApiError = exports.NetworkError = exports.RateLimitError = exports.ValidationError = exports.AuthenticationError = exports.SemanticPenError = void 0;
const types_1 = require("../types");
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
};
}
}
exports.SemanticPenError = SemanticPenError;
class AuthenticationError extends SemanticPenError {
constructor(message = 'Authentication failed', details) {
super(message, types_1.ErrorType.AUTHENTICATION_ERROR, details);
}
format() {
return `Authentication Error: ${this.message}`;
}
}
exports.AuthenticationError = AuthenticationError;
class ValidationError extends SemanticPenError {
constructor(message, field, value, details) {
super(message, types_1.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}`;
}
}
exports.ValidationError = ValidationError;
class RateLimitError extends SemanticPenError {
constructor(message = 'Rate limit exceeded', retryAfter, details) {
super(message, types_1.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;
}
}
exports.RateLimitError = RateLimitError;
class NetworkError extends SemanticPenError {
constructor(message, statusCode, endpoint, details) {
super(message, types_1.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;
}
}
exports.NetworkError = NetworkError;
class ApiError extends SemanticPenError {
constructor(message, statusCode, endpoint, response, details) {
super(message, types_1.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);
}
}
exports.ApiError = ApiError;
class TimeoutError extends SemanticPenError {
constructor(message, timeoutMs, operation, details) {
super(message, types_1.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;
}
}
exports.TimeoutError = TimeoutError;
class UnknownError extends SemanticPenError {
constructor(message = 'An unknown error occurred', originalError, details) {
super(message, types_1.ErrorType.UNKNOWN_ERROR, details);
this.originalError = originalError;
}
format() {
let formatted = `Unknown Error: ${this.message}`;
if (this.originalError) {
formatted += ` (Original: ${this.originalError.message})`;
}
return formatted;
}
}
exports.UnknownError = UnknownError;
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 });
}
}
exports.ErrorFactory = ErrorFactory;
//# sourceMappingURL=index.js.map