Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
RainaWLK opened this issue Jun 26, 2018 · 2 comments

Comments

@RainaWLK
Copy link

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.

@VenkatRamReddyK
Copy link

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;
  }
}

@rs
Copy link
Owner

rs commented Mar 29, 2021

This would break the API contract.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants