credix
Version:
Official SDK for Credix Credit Management System
109 lines • 3.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValidationError = exports.TimeoutError = exports.NetworkError = exports.CredixError = void 0;
/**
* Base error class for Credix
* Acts as a transparent proxy for API errors
*/
class CredixError extends Error {
constructor(message, statusCode, code, details, requestId) {
super(message);
this.name = 'CredixError';
this.code = code;
this.statusCode = statusCode;
this.details = details;
this.requestId = requestId;
this.timestamp = new Date();
// Maintains proper stack trace for where our error was thrown
if (Error.captureStackTrace) {
Error.captureStackTrace(this, CredixError);
}
}
/**
* Creates a CredixError from an HTTP response
* Transparently passes through API error information
*/
static fromResponse(statusCode, responseData, requestId) {
let message = 'An error occurred';
let code;
let details;
// Parse error response
if (responseData && typeof responseData === 'object') {
const data = responseData;
// Handle standard error response format
if (data.error && typeof data.error === 'object') {
const errorObj = data.error;
message = errorObj.message || message;
code = errorObj.code;
details = errorObj.details;
}
else if (data.message) {
message = data.message;
code = data.code;
}
}
return new CredixError(message, statusCode, code, details, requestId);
}
/**
* Creates a CredixError from an unknown error
*/
static fromUnknown(error, defaultMessage = 'An unexpected error occurred') {
if (error instanceof CredixError) {
return error;
}
if (error instanceof Error) {
return new CredixError(error.message || defaultMessage);
}
if (typeof error === 'string') {
return new CredixError(error);
}
return new CredixError(defaultMessage);
}
/**
* Serializes the error to a plain object
*/
toJSON() {
return {
name: this.name,
message: this.message,
code: this.code,
statusCode: this.statusCode,
details: this.details,
requestId: this.requestId,
timestamp: this.timestamp.toISOString(),
stack: this.stack,
};
}
}
exports.CredixError = CredixError;
/**
* Network-specific error
*/
class NetworkError extends CredixError {
constructor(message, details) {
super(message, undefined, 'NETWORK_ERROR', details);
this.name = 'NetworkError';
}
}
exports.NetworkError = NetworkError;
/**
* Timeout-specific error
*/
class TimeoutError extends CredixError {
constructor(message, timeout) {
super(message, undefined, 'TIMEOUT_ERROR', { timeout });
this.name = 'TimeoutError';
}
}
exports.TimeoutError = TimeoutError;
/**
* Validation-specific error
*/
class ValidationError extends CredixError {
constructor(message, field, value) {
super(message, 400, 'VALIDATION_ERROR', { field, value });
this.name = 'ValidationError';
}
}
exports.ValidationError = ValidationError;
//# sourceMappingURL=credix-error.js.map