fix: handle undefined safeStorage and remove diagnostic logging

Root cause: electron.safeStorage was undefined (not null) when the
property doesn't exist, causing "Cannot read properties of undefined"
error when accessing isEncryptionAvailable.

Fix: Normalize undefined to null with `|| null` operator when importing
safeStorage, ensuring consistent null checks throughout the code.

Changes:
- Set safeStorage to null if electron.safeStorage is undefined
- Remove all diagnostic try-catch blocks from settings UI
- Remove console.log debugging statements
- Restore clean code that now works correctly

This resolves the settings UI crash that prevented the API key
management section from displaying.
This commit is contained in:
2025-10-26 00:16:35 -04:00
parent efd1ff306e
commit 779b3d6e8c
2 changed files with 57 additions and 81 deletions

View File

@@ -61,7 +61,6 @@ export class MCPServerSettingTab extends PluginSettingTab {
// Authentication (Always Enabled) // Authentication (Always Enabled)
containerEl.createEl('h3', {text: 'Authentication'}); containerEl.createEl('h3', {text: 'Authentication'});
try {
const authDesc = containerEl.createEl('p', { const authDesc = containerEl.createEl('p', {
text: 'Authentication is required for all requests. Your API key is encrypted and stored securely using your system\'s credential storage.' text: 'Authentication is required for all requests. Your API key is encrypted and stored securely using your system\'s credential storage.'
}); });
@@ -70,30 +69,16 @@ export class MCPServerSettingTab extends PluginSettingTab {
authDesc.style.marginBottom = '16px'; authDesc.style.marginBottom = '16px';
// Show encryption status // Show encryption status
const encryptionAvailable = isEncryptionAvailable();
console.log('[Settings] Encryption available:', encryptionAvailable);
const encryptionStatus = containerEl.createEl('p', { const encryptionStatus = containerEl.createEl('p', {
text: encryptionAvailable text: isEncryptionAvailable()
? '🔒 Encryption: Available (using system keychain)' ? '🔒 Encryption: Available (using system keychain)'
: '⚠️ Encryption: Unavailable (API key stored in plaintext)' : '⚠️ Encryption: Unavailable (API key stored in plaintext)'
}); });
encryptionStatus.style.fontSize = '0.85em'; encryptionStatus.style.fontSize = '0.85em';
encryptionStatus.style.marginBottom = '12px'; encryptionStatus.style.marginBottom = '12px';
encryptionStatus.style.fontStyle = 'italic'; encryptionStatus.style.fontStyle = 'italic';
} catch (error) {
console.error('[Settings] Error in auth description section:', error);
containerEl.createEl('p', {
text: '⚠️ Error displaying authentication section: ' + (error instanceof Error ? error.message : String(error)),
cls: 'mod-error'
});
}
// API Key Display (always show - auth is always enabled) // API Key Display (always show - auth is always enabled)
console.log('[Settings] About to render API key section');
console.log('[Settings] API key length:', (this.plugin.settings.apiKey || '').length);
try {
new Setting(containerEl) new Setting(containerEl)
.setName('API Key Management') .setName('API Key Management')
.setDesc('Use this key in the Authorization header as Bearer token'); .setDesc('Use this key in the Authorization header as Bearer token');
@@ -141,15 +126,6 @@ export class MCPServerSettingTab extends PluginSettingTab {
keyDisplayContainer.style.marginBottom = '16px'; keyDisplayContainer.style.marginBottom = '16px';
keyDisplayContainer.textContent = this.plugin.settings.apiKey || ''; keyDisplayContainer.textContent = this.plugin.settings.apiKey || '';
console.log('[Settings] API key section rendered successfully');
} catch (error) {
console.error('[Settings] Error rendering API key section:', error);
containerEl.createEl('p', {
text: '⚠️ Error displaying API key: ' + (error instanceof Error ? error.message : String(error)),
cls: 'mod-error'
});
}
// MCP Client Configuration (show always, regardless of auth) // MCP Client Configuration (show always, regardless of auth)
containerEl.createEl('h3', {text: 'MCP Client Configuration'}); containerEl.createEl('h3', {text: 'MCP Client Configuration'});

View File

@@ -2,7 +2,7 @@
let safeStorage: any = null; let safeStorage: any = null;
try { try {
const electron = require('electron'); const electron = require('electron');
safeStorage = electron.safeStorage; safeStorage = electron.safeStorage || null;
} catch (error) { } catch (error) {
console.warn('Electron safeStorage not available, API keys will be stored in plaintext'); console.warn('Electron safeStorage not available, API keys will be stored in plaintext');
} }