redai-automation-web-sdk
Version:
TypeScript SDK for RedAI Automation Web API - Zalo Personal automation, messaging, advanced sticker search, and bulk operations. 100% compatible with automation-web backend. v1.8.0: Added SessionProxyService for managing proxy assignments to sessions with
583 lines • 31.1 kB
JavaScript
"use strict";
/**
* Error types and codes for RedAI Automation Web SDK
* Based on @warriorteam/zalo-personal SDK error handling patterns
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZaloErrorUtils = exports.ZaloErrorContext = exports.ERROR_MESSAGES = exports.ERROR_SEVERITY = exports.ERROR_CATEGORIES = exports.ZaloErrorSeverity = exports.ZaloErrorCode = void 0;
/**
* Zalo API Error Codes
* Based on @warriorteam/zalo-personal SDK and common API patterns
*/
var ZaloErrorCode;
(function (ZaloErrorCode) {
// Authentication & Session Errors (1000-1099)
ZaloErrorCode[ZaloErrorCode["SESSION_NOT_FOUND"] = 1001] = "SESSION_NOT_FOUND";
ZaloErrorCode[ZaloErrorCode["SESSION_EXPIRED"] = 1002] = "SESSION_EXPIRED";
ZaloErrorCode[ZaloErrorCode["SESSION_INACTIVE"] = 1003] = "SESSION_INACTIVE";
ZaloErrorCode[ZaloErrorCode["INVALID_CREDENTIALS"] = 1004] = "INVALID_CREDENTIALS";
ZaloErrorCode[ZaloErrorCode["AUTHENTICATION_FAILED"] = 1005] = "AUTHENTICATION_FAILED";
ZaloErrorCode[ZaloErrorCode["SESSION_CREATION_FAILED"] = 1006] = "SESSION_CREATION_FAILED";
ZaloErrorCode[ZaloErrorCode["SESSION_VALIDATION_FAILED"] = 1007] = "SESSION_VALIDATION_FAILED";
// User Information Errors (1100-1199)
ZaloErrorCode[ZaloErrorCode["USER_NOT_FOUND"] = 1101] = "USER_NOT_FOUND";
ZaloErrorCode[ZaloErrorCode["USER_ID_INVALID"] = 1102] = "USER_ID_INVALID";
ZaloErrorCode[ZaloErrorCode["USER_PROFILE_PRIVATE"] = 1103] = "USER_PROFILE_PRIVATE";
ZaloErrorCode[ZaloErrorCode["USER_BLOCKED_YOU"] = 1104] = "USER_BLOCKED_YOU";
ZaloErrorCode[ZaloErrorCode["USER_DOESNT_EXIST"] = 1105] = "USER_DOESNT_EXIST";
ZaloErrorCode[ZaloErrorCode["USER_NOT_ACCESSIBLE"] = 1106] = "USER_NOT_ACCESSIBLE";
// Friend Management Errors (1200-1299)
ZaloErrorCode[ZaloErrorCode["FRIEND_REQUEST_FAILED"] = 1201] = "FRIEND_REQUEST_FAILED";
ZaloErrorCode[ZaloErrorCode["FRIEND_NOT_FOUND"] = 1202] = "FRIEND_NOT_FOUND";
ZaloErrorCode[ZaloErrorCode["ALREADY_FRIENDS"] = 1203] = "ALREADY_FRIENDS";
ZaloErrorCode[ZaloErrorCode["FRIEND_REQUEST_SENT"] = 1204] = "FRIEND_REQUEST_SENT";
ZaloErrorCode[ZaloErrorCode["CANNOT_SEND_REQUEST"] = 1205] = "CANNOT_SEND_REQUEST";
ZaloErrorCode[ZaloErrorCode["FRIEND_BLOCKED"] = 1206] = "FRIEND_BLOCKED";
ZaloErrorCode[ZaloErrorCode["CANNOT_UNFRIEND"] = 1207] = "CANNOT_UNFRIEND";
// Group Management Errors (1300-1399)
ZaloErrorCode[ZaloErrorCode["GROUP_NOT_FOUND"] = 1301] = "GROUP_NOT_FOUND";
ZaloErrorCode[ZaloErrorCode["GROUP_ID_INVALID"] = 1302] = "GROUP_ID_INVALID";
ZaloErrorCode[ZaloErrorCode["NOT_GROUP_MEMBER"] = 1303] = "NOT_GROUP_MEMBER";
ZaloErrorCode[ZaloErrorCode["NOT_GROUP_ADMIN"] = 1304] = "NOT_GROUP_ADMIN";
ZaloErrorCode[ZaloErrorCode["GROUP_FULL"] = 1305] = "GROUP_FULL";
ZaloErrorCode[ZaloErrorCode["CANNOT_JOIN_GROUP"] = 1306] = "CANNOT_JOIN_GROUP";
ZaloErrorCode[ZaloErrorCode["GROUP_CREATION_FAILED"] = 1307] = "GROUP_CREATION_FAILED";
ZaloErrorCode[ZaloErrorCode["GROUP_SETTINGS_UPDATE_FAILED"] = 1308] = "GROUP_SETTINGS_UPDATE_FAILED";
// Message & Media Errors (1400-1499)
ZaloErrorCode[ZaloErrorCode["MESSAGE_NOT_FOUND"] = 1401] = "MESSAGE_NOT_FOUND";
ZaloErrorCode[ZaloErrorCode["CANNOT_SEND_MESSAGE"] = 1402] = "CANNOT_SEND_MESSAGE";
ZaloErrorCode[ZaloErrorCode["MEDIA_TOO_LARGE"] = 1403] = "MEDIA_TOO_LARGE";
ZaloErrorCode[ZaloErrorCode["UNSUPPORTED_MEDIA_TYPE"] = 1404] = "UNSUPPORTED_MEDIA_TYPE";
ZaloErrorCode[ZaloErrorCode["MESSAGE_SEND_FAILED"] = 1405] = "MESSAGE_SEND_FAILED";
ZaloErrorCode[ZaloErrorCode["MESSAGE_FORWARD_FAILED"] = 1406] = "MESSAGE_FORWARD_FAILED";
ZaloErrorCode[ZaloErrorCode["REACTION_FAILED"] = 1407] = "REACTION_FAILED";
// Request Validation Errors (1500-1599)
ZaloErrorCode[ZaloErrorCode["INVALID_PARAMETERS"] = 1501] = "INVALID_PARAMETERS";
ZaloErrorCode[ZaloErrorCode["MISSING_REQUIRED_FIELDS"] = 1502] = "MISSING_REQUIRED_FIELDS";
ZaloErrorCode[ZaloErrorCode["INVALID_USER_ID_FORMAT"] = 1503] = "INVALID_USER_ID_FORMAT";
ZaloErrorCode[ZaloErrorCode["INVALID_GROUP_ID_FORMAT"] = 1504] = "INVALID_GROUP_ID_FORMAT";
ZaloErrorCode[ZaloErrorCode["INVALID_MESSAGE_FORMAT"] = 1505] = "INVALID_MESSAGE_FORMAT";
ZaloErrorCode[ZaloErrorCode["BATCH_SIZE_EXCEEDED"] = 1506] = "BATCH_SIZE_EXCEEDED";
ZaloErrorCode[ZaloErrorCode["INVALID_ATTACHMENT_FORMAT"] = 1507] = "INVALID_ATTACHMENT_FORMAT";
// Rate Limiting Errors (1600-1699)
ZaloErrorCode[ZaloErrorCode["RATE_LIMIT_EXCEEDED"] = 1601] = "RATE_LIMIT_EXCEEDED";
ZaloErrorCode[ZaloErrorCode["TOO_MANY_REQUESTS"] = 1602] = "TOO_MANY_REQUESTS";
ZaloErrorCode[ZaloErrorCode["TEMPORARY_BLOCK"] = 1603] = "TEMPORARY_BLOCK";
ZaloErrorCode[ZaloErrorCode["REQUEST_LIMIT_REACHED"] = 1604] = "REQUEST_LIMIT_REACHED";
ZaloErrorCode[ZaloErrorCode["SPAM_DETECTED"] = 1605] = "SPAM_DETECTED";
// Network & Connection Errors (1700-1799)
ZaloErrorCode[ZaloErrorCode["NETWORK_ERROR"] = 1701] = "NETWORK_ERROR";
ZaloErrorCode[ZaloErrorCode["CONNECTION_TIMEOUT"] = 1702] = "CONNECTION_TIMEOUT";
ZaloErrorCode[ZaloErrorCode["API_UNAVAILABLE"] = 1703] = "API_UNAVAILABLE";
ZaloErrorCode[ZaloErrorCode["SERVER_ERROR"] = 1704] = "SERVER_ERROR";
ZaloErrorCode[ZaloErrorCode["CONNECTION_REFUSED"] = 1705] = "CONNECTION_REFUSED";
ZaloErrorCode[ZaloErrorCode["DNS_RESOLUTION_FAILED"] = 1706] = "DNS_RESOLUTION_FAILED";
ZaloErrorCode[ZaloErrorCode["PROXY_CONNECTION_FAILED"] = 1707] = "PROXY_CONNECTION_FAILED";
// Permission & Access Errors (1800-1899)
ZaloErrorCode[ZaloErrorCode["INSUFFICIENT_PERMISSIONS"] = 1801] = "INSUFFICIENT_PERMISSIONS";
ZaloErrorCode[ZaloErrorCode["ACCESS_DENIED"] = 1802] = "ACCESS_DENIED";
ZaloErrorCode[ZaloErrorCode["ACCOUNT_SUSPENDED"] = 1803] = "ACCOUNT_SUSPENDED";
ZaloErrorCode[ZaloErrorCode["ACCOUNT_BANNED"] = 1804] = "ACCOUNT_BANNED";
ZaloErrorCode[ZaloErrorCode["DEVICE_NOT_VERIFIED"] = 1805] = "DEVICE_NOT_VERIFIED";
ZaloErrorCode[ZaloErrorCode["REGION_RESTRICTED"] = 1806] = "REGION_RESTRICTED";
ZaloErrorCode[ZaloErrorCode["AGE_RESTRICTED"] = 1807] = "AGE_RESTRICTED";
// Business Logic Errors (1900-1999)
ZaloErrorCode[ZaloErrorCode["OPERATION_NOT_ALLOWED"] = 1901] = "OPERATION_NOT_ALLOWED";
ZaloErrorCode[ZaloErrorCode["FEATURE_NOT_AVAILABLE"] = 1902] = "FEATURE_NOT_AVAILABLE";
ZaloErrorCode[ZaloErrorCode["SUBSCRIPTION_REQUIRED"] = 1903] = "SUBSCRIPTION_REQUIRED";
ZaloErrorCode[ZaloErrorCode["QUOTA_EXCEEDED"] = 1904] = "QUOTA_EXCEEDED";
ZaloErrorCode[ZaloErrorCode["INVALID_OPERATION_CONTEXT"] = 1905] = "INVALID_OPERATION_CONTEXT";
// Data Processing Errors (2000-2099)
ZaloErrorCode[ZaloErrorCode["DATA_PARSING_FAILED"] = 2001] = "DATA_PARSING_FAILED";
ZaloErrorCode[ZaloErrorCode["ENCRYPTION_FAILED"] = 2002] = "ENCRYPTION_FAILED";
ZaloErrorCode[ZaloErrorCode["DECRYPTION_FAILED"] = 2003] = "DECRYPTION_FAILED";
ZaloErrorCode[ZaloErrorCode["DATA_CORRUPTION"] = 2004] = "DATA_CORRUPTION";
ZaloErrorCode[ZaloErrorCode["INVALID_JSON_FORMAT"] = 2005] = "INVALID_JSON_FORMAT";
// External Service Errors (2100-2199)
ZaloErrorCode[ZaloErrorCode["EXTERNAL_API_ERROR"] = 2101] = "EXTERNAL_API_ERROR";
ZaloErrorCode[ZaloErrorCode["WEBHOOK_DELIVERY_FAILED"] = 2102] = "WEBHOOK_DELIVERY_FAILED";
ZaloErrorCode[ZaloErrorCode["CALLBACK_TIMEOUT"] = 2103] = "CALLBACK_TIMEOUT";
ZaloErrorCode[ZaloErrorCode["THIRD_PARTY_SERVICE_ERROR"] = 2104] = "THIRD_PARTY_SERVICE_ERROR";
// Generic Errors (9000-9999)
ZaloErrorCode[ZaloErrorCode["UNKNOWN_ERROR"] = 9000] = "UNKNOWN_ERROR";
ZaloErrorCode[ZaloErrorCode["INTERNAL_ERROR"] = 9001] = "INTERNAL_ERROR";
ZaloErrorCode[ZaloErrorCode["INVALID_RESPONSE"] = 9002] = "INVALID_RESPONSE";
ZaloErrorCode[ZaloErrorCode["UNEXPECTED_ERROR"] = 9003] = "UNEXPECTED_ERROR";
ZaloErrorCode[ZaloErrorCode["NOT_IMPLEMENTED"] = 9004] = "NOT_IMPLEMENTED";
ZaloErrorCode[ZaloErrorCode["MAINTENANCE_MODE"] = 9005] = "MAINTENANCE_MODE";
})(ZaloErrorCode || (exports.ZaloErrorCode = ZaloErrorCode = {}));
/**
* Zalo Error Severity Levels
*/
var ZaloErrorSeverity;
(function (ZaloErrorSeverity) {
ZaloErrorSeverity["LOW"] = "low";
ZaloErrorSeverity["MEDIUM"] = "medium";
ZaloErrorSeverity["HIGH"] = "high";
ZaloErrorSeverity["CRITICAL"] = "critical";
})(ZaloErrorSeverity || (exports.ZaloErrorSeverity = ZaloErrorSeverity = {}));
/**
* Error Category Mappings
*/
exports.ERROR_CATEGORIES = {
[]: 'authentication',
[]: 'authentication',
[]: 'authentication',
[]: 'authentication',
[]: 'authentication',
[]: 'user_info',
[]: 'user_info',
[]: 'user_info',
[]: 'user_info',
[]: 'user_info',
[]: 'user_info',
[]: 'friend_management',
[]: 'friend_management',
[]: 'friend_management',
[]: 'friend_management',
[]: 'friend_management',
[]: 'friend_management',
[]: 'friend_management',
[]: 'group_management',
[]: 'group_management',
[]: 'group_management',
[]: 'group_management',
[]: 'group_management',
[]: 'group_management',
[]: 'group_management',
[]: 'group_management',
[]: 'messaging',
[]: 'messaging',
[]: 'messaging',
[]: 'messaging',
[]: 'messaging',
[]: 'messaging',
[]: 'messaging',
[]: 'validation',
[]: 'validation',
[]: 'validation',
[]: 'validation',
[]: 'validation',
[]: 'validation',
[]: 'validation',
[]: 'rate_limiting',
[]: 'rate_limiting',
[]: 'rate_limiting',
[]: 'rate_limiting',
[]: 'rate_limiting',
[]: 'network',
[]: 'network',
[]: 'network',
[]: 'network',
[]: 'network',
[]: 'network',
[]: 'network',
[]: 'permission',
[]: 'permission',
[]: 'permission',
[]: 'permission',
[]: 'permission',
[]: 'permission',
[]: 'permission',
[]: 'business_logic',
[]: 'business_logic',
[]: 'business_logic',
[]: 'business_logic',
[]: 'business_logic',
[]: 'data_processing',
[]: 'data_processing',
[]: 'data_processing',
[]: 'data_processing',
[]: 'data_processing',
[]: 'external_service',
[]: 'external_service',
[]: 'external_service',
[]: 'external_service',
[]: 'generic',
[]: 'generic',
[]: 'generic',
[]: 'generic',
[]: 'generic',
[]: 'generic',
};
/**
* Error Severity Mappings
*/
exports.ERROR_SEVERITY = {
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.LOW,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.LOW,
[]: ZaloErrorSeverity.LOW,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.LOW,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.LOW,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.CRITICAL,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.CRITICAL,
[]: ZaloErrorSeverity.CRITICAL,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.HIGH,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.CRITICAL,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.MEDIUM,
[]: ZaloErrorSeverity.LOW,
[]: ZaloErrorSeverity.HIGH,
};
/**
* Standardized Error Messages
*/
exports.ERROR_MESSAGES = {
// Authentication & Session Errors
[]: 'Session not found or inactive. Please login again.',
[]: 'Session has expired. Please login again.',
[]: 'Session is inactive. Please login again.',
[]: 'Invalid credentials provided. Please check your login information.',
[]: 'Authentication failed. Please try again.',
[]: 'Failed to create session. Please try again.',
[]: 'Session validation failed. Please login again.',
// User Information Errors
[]: 'User not found or does not exist.',
[]: 'Invalid user ID format provided.',
[]: 'User profile is private or not accessible.',
[]: 'User has blocked you or you have blocked this user.',
[]: 'User does not exist.',
[]: 'User information is not accessible.',
// Friend Management Errors
[]: 'Failed to send friend request.',
[]: 'Friend not found.',
[]: 'Users are already friends.',
[]: 'Friend request has already been sent.',
[]: 'Cannot send friend request at this time.',
[]: 'Friend request blocked due to privacy settings.',
[]: 'Cannot unfriend this user.',
// Group Management Errors
[]: 'Group not found.',
[]: 'Invalid group ID format.',
[]: 'You are not a member of this group.',
[]: 'You are not an admin of this group.',
[]: 'Group is full and cannot accept more members.',
[]: 'Cannot join this group.',
[]: 'Failed to create group.',
[]: 'Failed to update group settings.',
// Message & Media Errors
[]: 'Message not found.',
[]: 'Cannot send message at this time.',
[]: 'Media file is too large.',
[]: 'Unsupported media type.',
[]: 'Failed to send message.',
[]: 'Failed to forward message.',
[]: 'Failed to add reaction.',
// Request Validation Errors
[]: 'Invalid request parameters.',
[]: 'Missing required fields in request.',
[]: 'Invalid user ID format.',
[]: 'Invalid group ID format.',
[]: 'Invalid message format.',
[]: 'Batch size limit exceeded.',
[]: 'Invalid attachment format.',
// Rate Limiting Errors
[]: 'Rate limit exceeded. Please try again later.',
[]: 'Too many requests. Please slow down.',
[]: 'Account temporarily blocked. Please try again later.',
[]: 'Request limit reached. Please wait before trying again.',
[]: 'Spam activity detected. Please verify your actions.',
// Network & Connection Errors
[]: 'Network error occurred. Please check your connection.',
[]: 'Connection timeout. Please try again.',
[]: 'Zalo API is currently unavailable. Please try again later.',
[]: 'Zalo server error occurred. Please try again later.',
[]: 'Connection refused by server.',
[]: 'DNS resolution failed.',
[]: 'Proxy connection failed.',
// Permission & Access Errors
[]: 'Insufficient permissions to perform this action.',
[]: 'Access denied. You do not have permission to perform this action.',
[]: 'Account has been suspended. Please contact support.',
[]: 'Account has been banned. Please contact support.',
[]: 'Device not verified. Please verify your device.',
[]: 'This feature is not available in your region.',
[]: 'Age restricted content. Please verify your age.',
// Business Logic Errors
[]: 'Operation not allowed in current context.',
[]: 'Feature is not available.',
[]: 'Premium subscription required for this feature.',
[]: 'Daily/monthly quota exceeded. Please try again later.',
[]: 'Invalid operation context.',
// Data Processing Errors
[]: 'Failed to parse data.',
[]: 'Failed to encrypt data.',
[]: 'Failed to decrypt data.',
[]: 'Data corruption detected.',
[]: 'Invalid JSON format.',
// External Service Errors
[]: 'External API error occurred.',
[]: 'Webhook delivery failed.',
[]: 'Callback timeout occurred.',
[]: 'Third party service error.',
// Generic Errors
[]: 'An unknown error occurred.',
[]: 'Internal server error occurred.',
[]: 'Invalid response received.',
[]: 'An unexpected error occurred.',
[]: 'Feature not implemented yet.',
[]: 'Service is currently under maintenance.',
};
/**
* Error context builder helper
*/
class ZaloErrorContext {
constructor() {
this.details = {};
}
/**
* Set error code
*/
code(code) {
this.details.code = code;
this.details.category = exports.ERROR_CATEGORIES[code];
this.details.severity = exports.ERROR_SEVERITY[code];
this.details.message = exports.ERROR_MESSAGES[code];
return this;
}
/**
* Set custom message
*/
message(message) {
this.details.message = message;
return this;
}
/**
* Add context
*/
context(context) {
this.details.context = { ...this.details.context, ...context };
return this;
}
/**
* Set session ID
*/
sessionId(sessionId) {
this.details.sessionId = sessionId;
return this;
}
/**
* Set user ID
*/
userId(userId) {
this.details.userId = userId;
return this;
}
/**
* Set group ID
*/
groupId(groupId) {
this.details.groupId = groupId;
return this;
}
/**
* Set request ID
*/
requestId(requestId) {
this.details.requestId = requestId;
return this;
}
/**
* Set original error
*/
originalError(error) {
this.details.originalError = error;
return this;
}
/**
* Build the error details
*/
build() {
return {
code: this.details.code || ZaloErrorCode.UNKNOWN_ERROR,
message: this.details.message || exports.ERROR_MESSAGES[ZaloErrorCode.UNKNOWN_ERROR],
category: this.details.category || 'generic',
severity: this.details.severity || ZaloErrorSeverity.MEDIUM,
timestamp: Date.now(),
context: this.details.context,
originalError: this.details.originalError,
requestId: this.details.requestId,
sessionId: this.details.sessionId,
userId: this.details.userId,
groupId: this.details.groupId,
};
}
}
exports.ZaloErrorContext = ZaloErrorContext;
/**
* Utility functions for error handling
*/
class ZaloErrorUtils {
/**
* Create a standardized error response
*/
static createErrorResponse(code, customMessage, context) {
const errorContext = new ZaloErrorContext()
.code(code)
.message(customMessage || exports.ERROR_MESSAGES[code])
.context(context || {})
.build();
return {
success: false,
error: errorContext,
metadata: context,
};
}
/**
* Check if an error is recoverable
*/
static isRecoverable(errorCode) {
const unrecoverableCodes = [
ZaloErrorCode.ACCOUNT_BANNED,
ZaloErrorCode.AGE_RESTRICTED,
ZaloErrorCode.REGION_RESTRICTED,
ZaloErrorCode.DEVICE_NOT_VERIFIED,
];
return !unrecoverableCodes.includes(errorCode);
}
/**
* Check if an error should trigger a retry
*/
static shouldRetry(errorCode) {
const retryableCodes = [
ZaloErrorCode.NETWORK_ERROR,
ZaloErrorCode.CONNECTION_TIMEOUT,
ZaloErrorCode.API_UNAVAILABLE,
ZaloErrorCode.SERVER_ERROR,
ZaloErrorCode.RATE_LIMIT_EXCEEDED,
ZaloErrorCode.TEMPORARY_BLOCK,
];
return retryableCodes.includes(errorCode);
}
/**
* Get retry delay in milliseconds for a specific error code
*/
static getRetryDelay(errorCode) {
const retryDelays = {
[]: 60000, // 1 minute
[]: 300000, // 5 minutes
[]: 30000, // 30 seconds
[]: 5000, // 5 seconds
[]: 10000, // 10 seconds
[]: 30000, // 30 seconds
[]: 15000, // 15 seconds
};
return retryDelays[errorCode] || 5000; // Default 5 seconds
}
/**
* Format error for logging
*/
static formatForLogging(error) {
return `[${error.category.toUpperCase()}] ${error.code}: ${error.message}${error.sessionId ? ` (Session: ${error.sessionId})` : ''}${error.userId ? ` (User: ${error.userId})` : ''}${error.groupId ? ` (Group: ${error.groupId})` : ''}${error.requestId ? ` (Request: ${error.requestId})` : ''}`;
}
/**
* Check if error is session-related
*/
static isSessionError(errorCode) {
const sessionErrorCodes = [
ZaloErrorCode.SESSION_NOT_FOUND,
ZaloErrorCode.SESSION_EXPIRED,
ZaloErrorCode.SESSION_INACTIVE,
ZaloErrorCode.INVALID_CREDENTIALS,
ZaloErrorCode.AUTHENTICATION_FAILED,
];
return sessionErrorCodes.includes(errorCode);
}
/**
* Check if error is user-related
*/
static isUserError(errorCode) {
const userErrorCodes = [
ZaloErrorCode.USER_NOT_FOUND,
ZaloErrorCode.USER_ID_INVALID,
ZaloErrorCode.USER_PROFILE_PRIVATE,
ZaloErrorCode.USER_BLOCKED_YOU,
ZaloErrorCode.USER_DOESNT_EXIST,
ZaloErrorCode.USER_NOT_ACCESSIBLE,
];
return userErrorCodes.includes(errorCode);
}
/**
* Check if error is group-related
*/
static isGroupError(errorCode) {
const groupErrorCodes = [
ZaloErrorCode.GROUP_NOT_FOUND,
ZaloErrorCode.GROUP_ID_INVALID,
ZaloErrorCode.NOT_GROUP_MEMBER,
ZaloErrorCode.NOT_GROUP_ADMIN,
ZaloErrorCode.GROUP_FULL,
ZaloErrorCode.CANNOT_JOIN_GROUP,
ZaloErrorCode.GROUP_CREATION_FAILED,
ZaloErrorCode.GROUP_SETTINGS_UPDATE_FAILED,
];
return groupErrorCodes.includes(errorCode);
}
}
exports.ZaloErrorUtils = ZaloErrorUtils;
//# sourceMappingURL=error.types.js.map