Skip to content

Commit 222a1ce

Browse files
ahmadzeinclaude
andcommitted
fix: Add comprehensive debugging for port scan error
- Added detailed console logging throughout scan process - Check for window.portManager availability before scanning - Improved error messages with full stack traces - IPC handler now always returns array instead of throwing - Added validation for scan results This will help diagnose the "Cannot read properties of undefined" error 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 7f48690 commit 222a1ce

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

src/core/services/PortService.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ export class PortService {
158158
const platform = process.platform;
159159
let command: string;
160160

161+
console.log('PortService: Scanning on platform:', platform);
162+
161163
if (platform === 'darwin' || platform === 'linux') {
162164
command = 'lsof -i -P -n | grep LISTEN';
163165
} else if (platform === 'win32') {
@@ -167,9 +169,14 @@ export class PortService {
167169
}
168170

169171
try {
172+
console.log('PortService: Executing command:', command);
170173
const { stdout } = await execAsync(command);
171-
return this.parsePortScanOutput(stdout, platform);
174+
console.log('PortService: Command output length:', stdout.length);
175+
const ports = this.parsePortScanOutput(stdout, platform);
176+
console.log('PortService: Parsed ports:', ports.length);
177+
return ports;
172178
} catch (error) {
179+
console.error('PortService: Scan error:', error);
173180
// Command might fail if no ports are listening
174181
return [];
175182
}

src/gui/main/ipc/handlers.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,30 @@ export function setupIpcHandlers(): void {
5151

5252
ipcMain.handle('port:scan', async () => {
5353
try {
54-
return await portService.scanActivePorts();
54+
console.log('IPC: Scanning active ports...');
55+
const result = await portService.scanActivePorts();
56+
console.log('IPC: Scan result:', result);
57+
58+
// Ensure we always return an array
59+
if (!result) {
60+
console.log('IPC: No result, returning empty array');
61+
return [];
62+
}
63+
64+
if (!Array.isArray(result)) {
65+
console.error('IPC: Result is not an array:', typeof result, result);
66+
return [];
67+
}
68+
69+
return result;
5570
} catch (error) {
56-
throw new Error((error as Error).message);
71+
console.error('IPC: Scan error details:', {
72+
message: error instanceof Error ? error.message : 'Unknown error',
73+
stack: error instanceof Error ? error.stack : 'No stack trace',
74+
error
75+
});
76+
// Return empty array instead of throwing
77+
return [];
5778
}
5879
});
5980

src/gui/renderer/pages/ScanView.tsx

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ const ScanView: React.FC = () => {
1515
const scanPorts = async () => {
1616
setIsLoading(true);
1717
try {
18+
// Check if window.portManager exists
19+
if (!window.portManager) {
20+
console.error('window.portManager is undefined');
21+
message.error('Port Manager API not available');
22+
return;
23+
}
24+
25+
if (!window.portManager.port) {
26+
console.error('window.portManager.port is undefined');
27+
message.error('Port API not available');
28+
return;
29+
}
30+
31+
console.log('Starting port scan...');
1832
const ports = await window.portManager.port.scan();
1933
console.log('Scanned ports:', ports); // Debug log
2034

@@ -28,10 +42,22 @@ const ScanView: React.FC = () => {
2842

2943
setActivePorts(ports);
3044
setFilteredPorts(ports);
31-
message.success(`Found ${ports.length} active ports`);
45+
46+
if (ports.length === 0) {
47+
message.info('No active ports found');
48+
} else {
49+
message.success(`Found ${ports.length} active ports`);
50+
}
3251
} catch (error) {
33-
console.error('Scan error:', error);
34-
message.error(`Scan failed: ${(error as Error).message}`);
52+
console.error('Scan error full details:', {
53+
error,
54+
message: error instanceof Error ? error.message : 'Unknown error',
55+
stack: error instanceof Error ? error.stack : 'No stack',
56+
type: error?.constructor?.name
57+
});
58+
59+
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
60+
message.error(`Scan failed: ${errorMessage}`);
3561
setActivePorts([]);
3662
setFilteredPorts([]);
3763
} finally {

0 commit comments

Comments
 (0)