@j03fr0st/pubg-ts
Version:
A comprehensive TypeScript wrapper for the PUBG API
172 lines • 6.39 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PubgNetworkError = exports.PubgConfigurationError = exports.PubgAssetError = exports.PubgCacheError = exports.PubgValidationError = exports.PubgNotFoundError = exports.PubgAuthenticationError = exports.PubgRateLimitError = exports.PubgApiError = void 0;
/**
* Base PUBG API error class with enhanced context support
*/
class PubgApiError extends Error {
constructor(message, statusCode, response, context = {}) {
super(message);
this.statusCode = statusCode;
this.response = response;
this.name = 'PubgApiError';
this.correlationId = context.correlationId || this.generateCorrelationId();
this.timestamp = context.timestamp || Date.now();
this.context = {
correlationId: this.correlationId,
timestamp: this.timestamp,
operation: context.operation,
metadata: context.metadata || {},
stack: this.stack,
...context,
};
Object.setPrototypeOf(this, PubgApiError.prototype);
}
/**
* Generate a unique correlation ID for error tracking
*/
generateCorrelationId() {
return `pubg-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
}
/**
* Get detailed error information for debugging
*/
getDetails() {
return {
name: this.name,
message: this.message,
statusCode: this.statusCode,
correlationId: this.correlationId,
timestamp: this.timestamp,
context: this.context,
response: this.response,
};
}
/**
* Get a formatted error message with context
*/
getFormattedMessage() {
return `[${this.correlationId}] ${this.name}: ${this.message}`;
}
}
exports.PubgApiError = PubgApiError;
class PubgRateLimitError extends PubgApiError {
constructor(message = 'Rate limit exceeded', retryAfter, context = {}) {
super(message, 429, undefined, { ...context, operation: context.operation || 'rate_limit' });
this.retryAfter = retryAfter;
this.name = 'PubgRateLimitError';
Object.setPrototypeOf(this, PubgRateLimitError.prototype);
}
}
exports.PubgRateLimitError = PubgRateLimitError;
class PubgAuthenticationError extends PubgApiError {
constructor(message = 'Authentication failed', context = {}) {
super(message, 401, undefined, {
...context,
operation: context.operation || 'authentication',
});
this.name = 'PubgAuthenticationError';
Object.setPrototypeOf(this, PubgAuthenticationError.prototype);
}
}
exports.PubgAuthenticationError = PubgAuthenticationError;
class PubgNotFoundError extends PubgApiError {
constructor(message = 'Resource not found', context = {}) {
super(message, 404, undefined, {
...context,
operation: context.operation || 'resource_lookup',
});
this.name = 'PubgNotFoundError';
Object.setPrototypeOf(this, PubgNotFoundError.prototype);
}
}
exports.PubgNotFoundError = PubgNotFoundError;
class PubgValidationError extends PubgApiError {
constructor(message = 'Invalid request parameters', context = {}) {
super(message, 400, undefined, { ...context, operation: context.operation || 'validation' });
this.name = 'PubgValidationError';
Object.setPrototypeOf(this, PubgValidationError.prototype);
}
}
exports.PubgValidationError = PubgValidationError;
/**
* Cache-related error for cache operations
*/
class PubgCacheError extends PubgApiError {
constructor(message, cacheKey, operation, context = {}) {
super(message, 0, undefined, {
...context,
operation: `cache_${operation}`,
metadata: { cacheKey, cacheOperation: operation, ...context.metadata },
});
this.cacheKey = cacheKey;
this.operation = operation;
this.name = 'PubgCacheError';
Object.setPrototypeOf(this, PubgCacheError.prototype);
}
}
exports.PubgCacheError = PubgCacheError;
/**
* Asset management related errors
*/
class PubgAssetError extends PubgApiError {
constructor(message, assetId, assetType, context = {}) {
super(message, 0, undefined, {
...context,
operation: `asset_${assetType}`,
metadata: { assetId, assetType, ...context.metadata },
});
this.assetId = assetId;
this.assetType = assetType;
this.name = 'PubgAssetError';
Object.setPrototypeOf(this, PubgAssetError.prototype);
}
}
exports.PubgAssetError = PubgAssetError;
/**
* Configuration validation errors
*/
class PubgConfigurationError extends PubgApiError {
constructor(message, configField, expectedType, receivedValue, context = {}) {
super(message, 0, undefined, {
...context,
operation: 'configuration_validation',
metadata: {
configField,
expectedType,
receivedValue: typeof receivedValue,
receivedValueString: String(receivedValue),
...context.metadata,
},
});
this.configField = configField;
this.expectedType = expectedType;
this.receivedValue = receivedValue;
this.name = 'PubgConfigurationError';
Object.setPrototypeOf(this, PubgConfigurationError.prototype);
}
}
exports.PubgConfigurationError = PubgConfigurationError;
/**
* Network connectivity and communication errors
*/
class PubgNetworkError extends PubgApiError {
constructor(message, networkOperation, originalError, context = {}) {
super(message, 0, undefined, {
...context,
operation: `network_${networkOperation}`,
metadata: {
networkOperation,
originalErrorMessage: originalError?.message,
originalErrorName: originalError?.name,
...context.metadata,
},
});
this.networkOperation = networkOperation;
this.originalError = originalError;
this.name = 'PubgNetworkError';
Object.setPrototypeOf(this, PubgNetworkError.prototype);
}
}
exports.PubgNetworkError = PubgNetworkError;
//# sourceMappingURL=index.js.map