DEV Community

loading...

Telescope: Small Bug becomes a Feature? (3/3)

zg3d profile image Devansh Shah ・3 min read

Intro

The last blog I wrote was about the small bug I was fixing. This blog is about how that small bug fix turned into a feature with testing implemented. The function I wrote to solve one problem can now solve the root problem anywhere that needs to have a proper host and port URL. The issue I fixed it with the small fix mentioned in the last blog.

What I added

But, Like we talked about in the last post this would not fix the root issue. So, Implement a parse function module.

// parsesUrl to make sure there is only a single port
const parseUrl = (url, port) => {
  try {
    const urlObj = new URL(url);
    urlObj.port = port;
    return urlObj.href;
  } catch (e) {
    return null;
  }
};

module.exports = parseUrl;
Enter fullscreen mode Exit fullscreen mode

This one is also much more simple than the function in the last post.

const parseUrl = (url, port) => {
  let result;
  try {
    const urlObj = new URL(url);
    urlObj.port = port;
    if (urlObj.port !== '') {
      result = urlObj.href;
    }
  } catch (e) {
    result = null;
 }
return result;
}
Enter fullscreen mode Exit fullscreen mode

So, I learned that the IF statement is useless because if there is a double port I can just replace it with its self. If, there is no valid port we get an error that is caught and we return a null value. So, the new function only return either a valid URL or null.

I also added testing for the following scenarios

  • URL with port and valid port
  • URL is invalid
  • port is undefined
  • port is null
  • port is a number
  • port is a string
  • port is an invalid string
  • port is a number outside the port range (e.g., 65535 + 1)

Testing Reflection

I also learned from code review that the tests I write should be more isolated instead of me using higher scope variables I should encapsulate each test with its own testing variables. But, it was okay with this one because I made sure all my testing variables were immutable.

With this contribution I also update all legacy URL library use to instead use the current URL class. I also made sure that all code that was using an environmental variable to create a host+port URL used my new utility function.

More info on everything added/changed can be found here.

Contribution Reflection

With this contribution I got a greater understanding of NodeJS's URL library, testing with jest and going through the process of code reviews. Even though the bug was a simple fix it allowed me to get to the bottom of the real issue now if the function is used properly this type of bug will not happen as we fixed the real issue. So, we now do not care how someone has there environment variables setup with a port or without a port this fixes that issue.

What's next?

For telescope, I am still assigned on a issue to route all our logs to Kibana. This issue is taking a while to do because it requires a lot more research. I have tried to use a couple of solutions like filebeat to route the logs but this will probably take me a while to understand. The other thing I need to also make it work in dev and prod which will require greater knowledge of docker which I am still lacking but this issue is something I want to figure out and will try my best to solve it.

I am also working on trying to contribute to larger repos I have a current ask on the electron repo.

Discussion (0)

pic
Editor guide