dexpaprika-sdk
Version:
JavaScript SDK for the DexPaprika API
88 lines (87 loc) • 3.25 kB
JavaScript
;
/**
* Custom error classes for DexPaprika SDK
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeprecatedEndpointError = exports.ApiError = exports.PoolNotFoundError = exports.NetworkNotFoundError = exports.DexPaprikaError = void 0;
/**
* Base error class for all DexPaprika SDK errors
*/
class DexPaprikaError extends Error {
constructor(message) {
super(message);
this.name = 'DexPaprikaError';
}
}
exports.DexPaprikaError = DexPaprikaError;
/**
* Error thrown when a network is not found or invalid
*/
class NetworkNotFoundError extends DexPaprikaError {
constructor(networkId) {
super(`Network not found: ${networkId}`);
this.name = 'NetworkNotFoundError';
}
}
exports.NetworkNotFoundError = NetworkNotFoundError;
/**
* Error thrown when a pool is not found
*/
class PoolNotFoundError extends DexPaprikaError {
constructor(poolAddress) {
super(`Pool not found: ${poolAddress}`);
this.name = 'PoolNotFoundError';
}
}
exports.PoolNotFoundError = PoolNotFoundError;
/**
* General API error with status code
*/
class ApiError extends DexPaprikaError {
constructor(message, statusCode) {
super(`API Error (${statusCode}): ${message}`);
this.statusCode = statusCode;
this.name = 'ApiError';
}
}
exports.ApiError = ApiError;
/**
* Error thrown when trying to use a deprecated endpoint.
*
* When the API response body carries a "replacement" hint, it is surfaced on
* the {@link DeprecatedEndpointError.replacement} field and folded into the
* error message, so future deprecations self-document without an SDK change.
*/
class DeprecatedEndpointError extends DexPaprikaError {
constructor(endpoint, alternative, details = {}) {
const { replacement, apiMessage } = details;
const parts = [];
// Surface the API's own deprecation message first, when it sent one.
if (apiMessage) {
// Strip trailing sentence punctuation and whitespace without a
// backtracking regex (ReDoS-safe) and without trimEnd (ES2019+).
let trimmed = apiMessage.trim();
while (trimmed.length > 0 && '.:; \t\n\r\f\v'.indexOf(trimmed[trimmed.length - 1]) !== -1) {
trimmed = trimmed.slice(0, -1);
}
if (trimmed) {
parts.push(`${trimmed}.`);
}
}
parts.push(`The ${endpoint} endpoint has been deprecated and removed.`);
parts.push(`Please use ${alternative} instead.`);
// Point at the exact path the API advertised, if it differs from the
// human-facing suggestion above (avoids repeating it verbatim).
if (replacement && replacement !== alternative) {
parts.push(`API replacement: ${replacement}.`);
}
parts.push('For more information, visit: https://docs.dexpaprika.com/changelog/changelog');
super(parts.join(' '));
this.name = 'DeprecatedEndpointError';
this.endpoint = endpoint;
this.alternative = alternative;
this.replacement = replacement;
this.apiMessage = apiMessage;
}
}
exports.DeprecatedEndpointError = DeprecatedEndpointError;