|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function sanitizeErrorMessage(message) { |
|
|
if (typeof message !== 'string') { |
|
|
return message |
|
|
} |
|
|
|
|
|
|
|
|
let cleaned = message.replace(/https?:\/\/[^\s]+/gi, '') |
|
|
|
|
|
|
|
|
cleaned = cleaned.replace(/For more (?:details|information|help)[,\s]*/gi, '') |
|
|
cleaned = cleaned.replace(/(?:please\s+)?visit\s+\S*/gi, '') |
|
|
cleaned = cleaned.replace(/(?:see|check)\s+(?:our|the)\s+\S*/gi, '') |
|
|
cleaned = cleaned.replace(/(?:contact|reach)\s+(?:us|support)\s+at\s+\S*/gi, '') |
|
|
|
|
|
|
|
|
cleaned = cleaned.replace(/88code\S*/gi, '') |
|
|
cleaned = cleaned.replace(/duck\S*/gi, '') |
|
|
cleaned = cleaned.replace(/packy\S*/gi, '') |
|
|
cleaned = cleaned.replace(/ikun\S*/gi, '') |
|
|
cleaned = cleaned.replace(/privnode\S*/gi, '') |
|
|
cleaned = cleaned.replace(/yescode\S*/gi, '') |
|
|
cleaned = cleaned.replace(/share\S*/gi, '') |
|
|
cleaned = cleaned.replace(/yhlxj\S*/gi, '') |
|
|
cleaned = cleaned.replace(/gac\S*/gi, '') |
|
|
cleaned = cleaned.replace(/driod\S*/gi, '') |
|
|
|
|
|
cleaned = cleaned.replace(/\s+/g, ' ').trim() |
|
|
|
|
|
|
|
|
if (cleaned.length < 5) { |
|
|
return 'The requested model is currently unavailable' |
|
|
} |
|
|
|
|
|
return cleaned |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function sanitizeUpstreamError(errorData) { |
|
|
if (!errorData || typeof errorData !== 'object') { |
|
|
return errorData |
|
|
} |
|
|
|
|
|
|
|
|
const sanitized = JSON.parse(JSON.stringify(errorData)) |
|
|
|
|
|
|
|
|
const sanitizeObject = (obj) => { |
|
|
if (!obj || typeof obj !== 'object') { |
|
|
return obj |
|
|
} |
|
|
|
|
|
for (const key in obj) { |
|
|
if (key === 'message' && typeof obj[key] === 'string') { |
|
|
obj[key] = sanitizeErrorMessage(obj[key]) |
|
|
} else if (typeof obj[key] === 'object') { |
|
|
sanitizeObject(obj[key]) |
|
|
} |
|
|
} |
|
|
|
|
|
return obj |
|
|
} |
|
|
|
|
|
return sanitizeObject(sanitized) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function extractErrorMessage(body) { |
|
|
if (!body) { |
|
|
return '' |
|
|
} |
|
|
|
|
|
|
|
|
if (typeof body === 'string') { |
|
|
const trimmed = body.trim() |
|
|
if (!trimmed) { |
|
|
return '' |
|
|
} |
|
|
try { |
|
|
const parsed = JSON.parse(trimmed) |
|
|
return extractErrorMessage(parsed) |
|
|
} catch (error) { |
|
|
return trimmed |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (typeof body === 'object') { |
|
|
|
|
|
if (typeof body.error === 'string') { |
|
|
return body.error |
|
|
} |
|
|
|
|
|
if (body.error && typeof body.error === 'object') { |
|
|
if (typeof body.error.message === 'string') { |
|
|
return body.error.message |
|
|
} |
|
|
if (typeof body.error.error === 'string') { |
|
|
return body.error.error |
|
|
} |
|
|
} |
|
|
|
|
|
if (typeof body.message === 'string') { |
|
|
return body.message |
|
|
} |
|
|
} |
|
|
|
|
|
return '' |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isAccountDisabledError(statusCode, body) { |
|
|
if (statusCode !== 400) { |
|
|
return false |
|
|
} |
|
|
|
|
|
const message = extractErrorMessage(body) |
|
|
if (!message) { |
|
|
return false |
|
|
} |
|
|
|
|
|
const lowerMessage = message.toLowerCase() |
|
|
|
|
|
return ( |
|
|
lowerMessage.includes('organization has been disabled') || |
|
|
lowerMessage.includes('account has been disabled') || |
|
|
lowerMessage.includes('account is disabled') || |
|
|
lowerMessage.includes('no account supporting') || |
|
|
lowerMessage.includes('account not found') || |
|
|
lowerMessage.includes('invalid account') || |
|
|
lowerMessage.includes('too many active sessions') |
|
|
) |
|
|
} |
|
|
|
|
|
module.exports = { |
|
|
sanitizeErrorMessage, |
|
|
sanitizeUpstreamError, |
|
|
extractErrorMessage, |
|
|
isAccountDisabledError |
|
|
} |
|
|
|