@botwall/sdk
Version:
BotWall SDK for site protection and bot crawling
81 lines • 2.61 kB
JavaScript
;
// BotWall SDK Error Classes
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValidationError = exports.NetworkError = exports.InsufficientCreditsError = exports.InvalidCredentialsError = exports.BotWallSDKError = void 0;
exports.createErrorFromResponse = createErrorFromResponse;
class BotWallSDKError extends Error {
constructor(error) {
super(error.message);
this.name = 'BotWallSDKError';
this.code = error.code;
this.status = error.status;
this.details = error.details;
}
}
exports.BotWallSDKError = BotWallSDKError;
class InvalidCredentialsError extends BotWallSDKError {
constructor(message = 'Invalid bot or site credentials') {
super({
code: 'INVALID_CREDENTIALS',
message,
status: 401
});
this.name = 'InvalidCredentialsError';
}
}
exports.InvalidCredentialsError = InvalidCredentialsError;
class InsufficientCreditsError extends BotWallSDKError {
constructor(message = 'Insufficient credits to perform this action') {
super({
code: 'INSUFFICIENT_CREDITS',
message,
status: 402
});
this.name = 'InsufficientCreditsError';
}
}
exports.InsufficientCreditsError = InsufficientCreditsError;
class NetworkError extends BotWallSDKError {
constructor(message = 'Network error occurred', details) {
super({
code: 'NETWORK_ERROR',
message,
details
});
this.name = 'NetworkError';
}
}
exports.NetworkError = NetworkError;
class ValidationError extends BotWallSDKError {
constructor(message = 'Validation error', details) {
super({
code: 'VALIDATION_ERROR',
message,
details
});
this.name = 'ValidationError';
}
}
exports.ValidationError = ValidationError;
// Helper function to create errors from HTTP responses
async function createErrorFromResponse(response) {
let message;
try {
const errorData = await response.json();
message = errorData.message || `HTTP ${response.status}`;
}
catch {
message = `HTTP ${response.status}: ${response.statusText}`;
}
switch (response.status) {
case 401:
return new InvalidCredentialsError(message);
case 402:
return new InsufficientCreditsError(message);
case 400:
return new ValidationError(message);
default:
return new NetworkError(message);
}
}
//# sourceMappingURL=errors.js.map