fix(init): add defensive error handling for edge cases

Added comprehensive error handling to prevent extension initialization failures:

- Added settings validation in loadSettings() to detect corrupt data
- Improved error recovery in main initialization with granular try-catch blocks
- Enhanced HTML regex import with structure validation and detailed error logging
- Added detection for conflicting old manual formatting regex scripts
- Added user-friendly toastr notifications for initialization errors and conflicts
- Each init step now has independent error handling to prevent cascade failures

This fixes issues where invalid extension_settings could prevent the extension
from loading entirely. The extension will now gracefully handle corrupt data,
warn about conflicts, and fall back to defaults when necessary.

Related to user report where extension wouldn't load with certain settings.json
configurations containing old manual formatting regexes or malformed data.
This commit is contained in:
Lucas 'Paperboy' Rose-Winters
2025-10-18 16:01:10 +11:00
parent 0f2e19fc91
commit d9b784d745
4 changed files with 209 additions and 37 deletions
+53 -2
View File
@@ -3,6 +3,37 @@
* Automatically imports HTML cleaning regex to strip HTML tags from outgoing prompts
*/
/**
* Detects old manual regex formatters that might conflict with the extension
* @param {Object} st_extension_settings - SillyTavern extension settings object
* @returns {Array<string>} Array of conflicting regex script names
*/
export function detectConflictingRegexScripts(st_extension_settings) {
const conflictingNames = [
'Format User\'s Stats (Only Output)',
'Format User\'s Stats (Only Display)',
'Format Info Box (Only Output)',
'Format Info Box (Only Display)',
'Format Character\'s Thoughts (Only Output)',
'Format Character\'s Thoughts (Only Display)',
'Format Character Thoughts (Only Output)',
'Format Character Thoughts (Only Display)'
];
const existingScripts = st_extension_settings?.regex || [];
const conflicts = [];
for (const script of existingScripts) {
if (script && script.scriptName && conflictingNames.some(name =>
script.scriptName.toLowerCase().includes(name.toLowerCase())
)) {
conflicts.push(script.scriptName);
}
}
return conflicts;
}
/**
* Automatically imports the HTML cleaning regex script if it doesn't already exist.
* This regex removes HTML tags from outgoing prompts to prevent formatting issues.
@@ -11,10 +42,25 @@
*/
export async function ensureHtmlCleaningRegex(st_extension_settings, saveSettingsDebounced) {
try {
// Validate extension settings structure
if (!st_extension_settings || typeof st_extension_settings !== 'object') {
console.warn('[RPG Companion] Invalid extension_settings object, skipping HTML regex import');
return;
}
// Check if the HTML cleaning regex already exists
const scriptName = 'Clean HTML (From Outgoing Prompt)';
const existingScripts = st_extension_settings?.regex || [];
const alreadyExists = existingScripts.some(script => script.scriptName === scriptName);
// Validate regex array
if (!Array.isArray(existingScripts)) {
console.warn('[RPG Companion] extension_settings.regex is not an array, resetting to empty array');
st_extension_settings.regex = [];
}
const alreadyExists = existingScripts.some(script =>
script && typeof script === 'object' && script.scriptName === scriptName
);
if (alreadyExists) {
console.log('[RPG Companion] HTML cleaning regex already exists, skipping import');
@@ -55,11 +101,16 @@ export async function ensureHtmlCleaningRegex(st_extension_settings, saveSetting
st_extension_settings.regex.push(regexScript);
// Save the changes using the already-imported function
saveSettingsDebounced();
if (typeof saveSettingsDebounced === 'function') {
saveSettingsDebounced();
} else {
console.warn('[RPG Companion] saveSettingsDebounced is not a function, cannot save HTML regex');
}
console.log('[RPG Companion] ✅ HTML cleaning regex imported successfully');
} catch (error) {
console.error('[RPG Companion] Failed to import HTML cleaning regex:', error);
console.error('[RPG Companion] Error details:', error.message, error.stack);
// Don't throw - this is a nice-to-have feature
}
}