node-netmask icon indicating copy to clipboard operation
node-netmask copied to clipboard

Return undefined or null instead of throw a error when input ip addr is invalid

Open RainaWLK opened this issue 7 years ago • 2 comments

If input ip addr or netmask is invalid, it will throw error, then I must use nested try / catch to handle that. It makes code complicated. I suggest return a undefined / null value for calculate error. Like behavior of many JS native functions.

RainaWLK avatar Jun 26 '18 09:06 RainaWLK

I also feel if this is handled in the node-netmask as mentioned by @RainaWLK it would be great. Alternatively, we could handle it on our end as mentioned in the code below, but we need to add another wrapper around Netmask to handle those cases.

function getNetMaskStatus({ipAddress, gateway, subnetMask}) {
  // ipAddress and gateway cannot be the same
  let netMaskStatus = {validIpAddress: false, validGateway: false, error: null};
  try {
    const block = new Netmask(ipAddress, subnetMask);
    netMaskStatus.validIpAddress = true;
    Log.info('block: ', block);
    //ipAddress and gateway cannot be the same. gateway ip should be changed.
    if (ipAddress === gateway) {
      return netMaskStatus;
    }
    const gatewayisInSubnetRange = block.contains(gateway)
    if (gatewayisInSubnetRange) {
      netMaskStatus.validGateway = true;
      return netMaskStatus;
    }
    else {
      return netMaskStatus;
    }
  } catch (error) {
    Log.error('Net mask Status: ',netMaskStatus);
    Log.error('NetMask error: ', error);
    netMaskStatus.error = error;
    return netMaskStatus;
  }
}

VenkatRamReddyK avatar Mar 09 '20 00:03 VenkatRamReddyK

This would break the API contract.

rs avatar Mar 29 '21 20:03 rs