@chiraitori/hoyolab-core
Version:
Core utilities for HoYoLab automation - daily check-ins and code redemption with smart rate limiting
53 lines (48 loc) • 1.61 kB
JavaScript
class HoyoLabError extends Error {
constructor(message, code, details = {}) {
super(message);
this.name = 'HoyoLabError';
this.code = code;
this.details = details;
}
}
const ErrorCodes = {
INVALID_COOKIE: 'INVALID_COOKIE',
ALREADY_CHECKED_IN: 'ALREADY_CHECKED_IN',
INVALID_CODE: 'INVALID_CODE',
CODE_EXPIRED: 'CODE_EXPIRED',
CODE_ALREADY_USED: 'CODE_ALREADY_USED',
RATE_LIMITED: 'RATE_LIMITED',
NETWORK_ERROR: 'NETWORK_ERROR',
API_ERROR: 'API_ERROR',
INVALID_UID: 'INVALID_UID',
ACCOUNT_NOT_FOUND: 'ACCOUNT_NOT_FOUND'
};
const createError = (code, message, details = {}) => {
return new HoyoLabError(message, code, details);
};
const handleApiError = (retcode, message) => {
switch (retcode) {
case -100:
return createError(ErrorCodes.INVALID_COOKIE, 'Invalid or expired cookie');
case -5003:
return createError(ErrorCodes.ALREADY_CHECKED_IN, 'Already checked in today');
case -2001:
case -2003:
return createError(ErrorCodes.INVALID_CODE, 'Invalid or expired redemption code');
case -2017:
return createError(ErrorCodes.CODE_ALREADY_USED, 'Redemption code already used');
case -2018:
return createError(ErrorCodes.CODE_EXPIRED, 'Redemption code has expired');
case 1034:
return createError(ErrorCodes.RATE_LIMITED, 'Rate limited, please try again later');
default:
return createError(ErrorCodes.API_ERROR, message || `API error: ${retcode}`);
}
};
module.exports = {
HoyoLabError,
ErrorCodes,
createError,
handleApiError
};