Skip to content

Commit

Permalink
Better error handling when getting IP addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrancis committed Oct 10, 2024
1 parent fcbcb7b commit 514ff5b
Showing 1 changed file with 37 additions and 16 deletions.
53 changes: 37 additions & 16 deletions src/platforms/linux-ubuntu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,40 @@ class LinuxUbuntuPlatform extends BasePlatform {
});
}

/**
* Get the IP address of the first Ethernet adapter.
*
* @returns {Promise<string>} A Promise which resolves with the IP address as
* a string or an empty string.
*/
private async getEthernetIpAddress(): Promise<string> {
return this.getEthernetDevices().then((ethernetDevices) => {
return this.getDeviceIp4Config(ethernetDevices[0]);
}).then((ethernetIp4Config) => {
return ethernetIp4Config[0].address;
}).catch(() => {
console.log(`Unable to get detect Ethernet IP address.`);
return '';
});
}

/**
* Get the IP address of the first Wi-Fi adapter.
*
* @returns {Promise<string>} A Promise which resolves with the IP address as
* a string or an empty string.
*/
private async getWifiIpAddress(): Promise<string> {
return this.getWifiDevices().then((wifiDevices) => {
return this.getDeviceIp4Config(wifiDevices[0]);
}).then((wifiIp4Config) => {
return wifiIp4Config[0].address;
}).catch(() => {
console.log(`Unable to get detect Wi-Fi IP address.`);
return '';
});
}

/**
* Get the LAN mode and options.
*
Expand Down Expand Up @@ -264,29 +298,16 @@ class LinuxUbuntuPlatform extends BasePlatform {
* }
*/
async getNetworkAddressesAsync(): Promise<NetworkAddresses> {
// TODO: Handle the case where there is no Ethernet adapter or no Wi-Fi
// adapter or one of them isn't assigned an IP
let result: NetworkAddresses = {
lan: '',
wlan: {
ip: '',
ssid: ''
}
};
return this.getEthernetDevices().then((ethernetDevices) => {
return this.getDeviceIp4Config(ethernetDevices[0]);
}).then((ethernetIp4Config) => {
result.lan = ethernetIp4Config[0].address;
return this.getWifiDevices();
}).then((wifiDevices) => {
return this.getDeviceIp4Config(wifiDevices[0]);
}).then((wifiIp4Config) => {
result.wlan.ip = wifiIp4Config[0].address;
return result;
}).catch((error) => {
console.error('Error getting IP addresses from NetworkManager: ' + error);
return result;
});
result.lan = await this.getEthernetIpAddress();
result.wlan.ip = await this.getWifiIpAddress();
return result;
}

// Currently unused code...
Expand Down

0 comments on commit 514ff5b

Please sign in to comment.