Skip to content

FIX-1: Slight refactor to make the error handling more robust #3

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

Merged
merged 4 commits into from
Apr 25, 2024

Conversation

NickAnderegg
Copy link
Collaborator

No description provided.

@NickAnderegg NickAnderegg added the CTO.ai Review Automated CTO.ai bot code review label Apr 24, 2024
}

weather(queryParams.lat, queryParams.lon, queryParams.timezone).then((weatherData) => {
res.json(weatherData)
}).catch((e) => {
res.json({status: 'error', code: 500, message: e.message})
})
})

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pull request generally is well-structured, with clear comments. However, there are a few potential issues that could be improved.

  1. It seems that requestCityFromIP and requestCoordsFromIP from ./src/geoipAPI.js have been replaced by single function geolocateFromIP. If those functions were doing different things, this might cause problems. Review please if the geolocateFromIP function fully replaces both previous methods.

  2. The weather API call in api.get('/') uses coords.lat, coords.lon, and coords.timezone. If these properties don't exist or if the geolocation completely fails (maybe due to an unsuccessful geolocation because of no internet etc.), you would potentially execute erroneous weather API call, even though there is catch block below.

  3. Misleading comments: if ("city" in coords) and so on imply that these properties can be missing. Yet your code further down assumes they always exist during the weather API call.

  4. Ensure correct error handling: There's mixed use of Promise .then and .catch chains and try-catch blocks.

  5. In api.get('/weather'), there is a condition verifying query parameters. However, there isn't any form of validation for them. Latitudes and longitudes should fall within specific ranges, and timezone should typically match a certain format or be part of a list of standard time zones.

  6. Make sure the front-end implements appropriate behavior when 'status' comes back as 'error'. This back-end design expects the client to be able handle responses of different structures.

requestCoordsFromIP,
// requestCityFromIP,
// requestCoordsFromIP,
geolocateFromIP,
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are the potential issues I've identified in this patch:

  1. Typos/Missing Variables: There's no declaration or definition of ipAddrRegex, which is used to validate the IP address. If this variable is not defined elsewhere in the program, it will raise an error.

  2. Function Overhaul: The function requestCoordsFromIP has been commented out and replaced with geolocateFromIP. If there are other parts of the code that depend on calling requestCoordsFromIP, these calls will fail as the function is no longer exported from this module. Ensure to update these calls if necessary.

  3. Breaking change in Error Handling: The original code was rejecting the promise with the error when API request failed, or response parsing failed, thereby allowing the caller function to catch and handle these failures appropriately. However, the new code is rejecting the promise with defaultCoords object, which is a success scenario object. Therefore, the caller function might interpret this as a successful API call and proceed with incorrect coordinates. If you intend to provide defaults as a fallback mechanism for failure scenarios, you should resolve rather than reject the promise with default values.

  4. Input validation: Even though commenting out the requestCityFromIP function does not directly pose problems, it slightly reduces input validation granularity. Previously separate functions validated the IP addresses and handled different types of requests. Now, they are combined which reduces modularity.

  5. Lat and Lon values in defaultCoords: These values are strings ('43.7', '-79.42') instead of numbers (43.7, -79.42). If the rest of your program expects these to be numbers, this may cause inconsistencies or errors downstream.

Ensure you test these changes well, especially considering situations where things don't go to plan such as network errors, API down times, invalid responses etc.

console.log('------------------------------\nQuery parameters:')
console.log(queryParams)
// console.log('------------------------------\nQuery parameters:')
// console.log(queryParams)

const queryString = Object.keys(queryParams).map(key => key + '=' + queryParams[key]).join('&')

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided patch primarily includes commenting out console.log lines. What this would achieve is the reduction of console output when these functions are called. If those logs were only there for debugging purposes and aren't needed in production code, it's legitimate to remove them.

However, this will not produce any logging output from those points in the codebase anymore. This might be a problem if the logging information was providing valuable insights about the application's state during runtime. For instance, in case of bugs or other issues in production, these logs could be very helpful for troubleshooting, even more so when no other robust monitoring or error tracking system is in place.

In terms of functionality, assuming the other parts of these functions don't have issues, this shouldn't introduce any bugs or exceptions as all it’s doing is commenting out log output. There aren't any typos, incorrect usage of variables or unhandled scenarios in the changed lines according to your description.

However, please note that without having access to the whole codebase, as well as knowledge of the specific context, requirements, and environment where the software will be deployed, pinpointing every possible risk or issue introduced by a patch will be somewhat limited.

@@ -2,7 +2,7 @@ h3 Weather Forecast
button(id="geolocate") Get Local Forecast
p(id="coords")
#forecast
if forecast.current
if forecast && forecast.current
if forecast.current.temperature_2m
#currentTemperature
span(id="currentTempVal") #{forecast.current.temperature_2m[0]}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided patch appears to be correct, as it adds a nil check for forecast before attempting to access its current attribute. This should help prevent any potential NoMethodError exceptions from occurring if forecast is nil.

However, based on the diff provided, there's no change in handling when forecast.current.temperature_2m is nil or absent. If temperature_2m is undefined or nil, then #{forecast.current.temperature_2m[0]} might throw an error similar to "undefined method `[0]' for nil:NilClass". It might be good to add a condition to handle this case.

There's also no check for forecast.current.temperature_2m being an empty array before trying to get its first element [0]. If temperature_2m is an empty array, #{forecast.current.temperature_2m[0]} would give us nil. Depending how you handle nil in your output, this could be a problem or not.

Remember that this review is limited to the diff provided and cannot account for issues elsewhere in the codebase.



module.exports = {
requestCityFromIP,
requestCoordsFromIP,
geolocateFromIP,
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few potential issues with the patch:

  1. Rename of requestCoordsFromIP: The function requestCoordsFromIP has been renamed to geolocateFromIP, this will break any other parts of the code that still uses the original name, unless they are changed as well.

  2. Unused constant defaultCoords: The defaultCoords object is created, but it is never used when the API call fails. When an exception is thrown or rejected in the promise callback for the request, it rejects with this default coordinates object. This might not be what you want if the goal is to handle errors effectively. An error object containing helpful debug information should usually be returned.

  3. Changing error handling behavior: The previous version was rejecting the promise with the caught error, which allows the caller to take the appropriate actions based on the specifics of the error. The new version rejects with the defaultCoords object indiscriminately for all types of errors, which could mask a wide variety of problems and make debugging difficult, especially for callers that expect to receive an Error object upon rejection.

  4. Loss of functionality: The requestCityFromIP function was removed. If there are places in the existing codebase where this function is invoked, those will break after applying this patch.

  5. Latitude and Longitude data type inconsistency: In defaultCoords, lat and lon values are strings, while the API likely returns them as numbers. This difference can lead to unexpected behaviors down the line wherever these values are consumed in calculations.

Overall, it would be important to consider these potential issues before merging the pull request, possibly leaving comments requesting changes or further clarification.

@NickAnderegg NickAnderegg merged commit 35aaeb4 into feat-2 Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CTO.ai Review Automated CTO.ai bot code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant