friedrith / node-wifi

📶 NodeJS tool to manage wifi (connections, scans)
MIT License
396 stars 161 forks source link

Inconsistent error reporting #149

Closed joezappie closed 1 year ago

joezappie commented 3 years ago

I am trying to show user friendly error messages to the user when they try to connect to a WiFi network. If they type in the SSID incorrect, I get an error that has a code 10. If the user types the SSID correct, but provides the wrong secret, no code is given.

socket.on('wifi.set', async(data, callback) => {
  try {
    await wifi.connect({ssid: data.ssid, password: data.password });
  } catch(err) {
    console.log(err);
  }
});

Expected Behavior

I would expect the errors to all follow the same format, providing a code or a way to decipher if its an unknown SSID or bad password via code.

Current Behavior

Currently, bad SSIDs report an error with killed, code, signal and cmd:

Error: No network with SSID 'RC Engineering 2.4GH' found.

    at ChildProcess.exithandler (child_process.js:308:12)
    at ChildProcess.emit (events.js:314:20)
    at maybeClose (internal/child_process.js:1021:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5) {
  killed: false,
  code: 10,
  signal: null,
  cmd: 'nmcli -w 10 device wifi connect RC Engineering 2.4GH password dsf ifname wlan0'
}

Good SSID but bad password reports no error code:

Error: Connection activation failed: (7) Secrets were required, but not provided.

    at /var/www/node_modules/node-wifi/src/linux-connect.js:23:13
    at ChildProcess.exithandler (child_process.js:299:7)
    at ChildProcess.emit (events.js:314:20)
    at maybeClose (internal/child_process.js:1021:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5)

Another example where a code is given is invalid permissions:

Error: Command failed: nmcli -w 10 device wifi connect RC Engineering 2.4GHz password dsf ifname wlan0
Error: Failed to add/activate new connection: Not authorized to control networking.

    at ChildProcess.exithandler (child_process.js:308:12)
    at ChildProcess.emit (events.js:314:20)
    at maybeClose (internal/child_process.js:1021:16)
    at Socket.<anonymous> (internal/child_process.js:443:11)
    at Socket.emit (events.js:314:20)
    at Pipe.<anonymous> (net.js:676:12) {
  killed: false,
  code: 4,
  signal: null,
  cmd: 'nmcli -w 10 device wifi connect RC Engineering 2.4GHz password dsf ifname wlan0'
}

Affected features

Possible Solution

Add consistent formatting to any thrown errors - custom error class?

I could do a string search for something like "secrets were required" but that could easily be broken. Having a code that wont change would be best.

Steps to Reproduce (for bugs)

  1. Call the .connect function with a bad SSID
  2. Call the .connect function with a good SSID but bad password
  3. Note that error messages are different format.
  4. Context

This would help developers better be able to integrate node-wifi into their app by allowing them to more easily code what to do depending on the outcome of a task. Theres already a code for the "no network" error, and I'm sure theres other scenarios where an error is thrown that would be useful to have a list of possible error codes.

Your Environment

friedrith commented 4 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.