-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
51fe840
8c306a6
8eb8294
35aaeb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,16 @@ function buildAPIRequest(ipAddr, fieldsVal) { | |
|
||
} | ||
|
||
const defaultCoords = { | ||
status: 'success', | ||
country: 'Canada', | ||
regionName: 'Ontario', | ||
city: 'Toronto', | ||
lat: '43.7', | ||
lon: '-79.42', | ||
timezone: 'America/Toronto', | ||
} | ||
|
||
function apiRequest(url) { | ||
|
||
return new Promise((resolve, reject) => { | ||
|
@@ -49,6 +59,26 @@ function apiRequest(url) { | |
res.on('end', () => { | ||
try { | ||
coordsResponse = JSON.parse(responseData) | ||
// console.log("coordsResponse: ", coordsResponse) | ||
|
||
let locationName = '' | ||
if ("regionName" in coordsResponse && "city" in coordsResponse && "country" in coordsResponse) { | ||
locationName = `${coordsResponse.city}, ${coordsResponse.regionName}, ${coordsResponse.country}` | ||
} else if ("country" in coordsResponse) { | ||
if ("city" in coordsResponse) { | ||
locationName = `${coordsResponse.city}, ${coordsResponse.country}` | ||
} else if ("regionName" in coordsResponse) { | ||
locationName = `${coordsResponse.regionName}, ${coordsResponse.country}` | ||
} else { | ||
locationName = coordsResponse.country | ||
} | ||
} else if ("city" in coordsResponse) { | ||
locationName = coordsResponse.city | ||
} else { | ||
locationName = coordsResponse.regionName | ||
} | ||
|
||
coordsResponse.locationName = locationName | ||
|
||
// Because this sample app is used in live demos, we want to | ||
// anonymize the coordinates returned to the client side. | ||
|
@@ -58,13 +88,17 @@ function apiRequest(url) { | |
} | ||
|
||
if (coordsResponse.status !== 'success') { | ||
throw new Error('API request to `ip-api.com` failed.') | ||
// If our query to the geolocation API endpoint fails, | ||
// return the default coordinates object, which has the | ||
// coordinates and timezone for Toronto, Ontario, Canada. | ||
console.error('API request to `ip-api.com` did not succeed. Rejecting with default coordinates object (Toronto).') | ||
reject(defaultCoords) | ||
} else { | ||
resolve(coordsResponse) | ||
} | ||
} catch (e) { | ||
console.error(e.message) | ||
reject(e) | ||
reject(defaultCoords) | ||
} | ||
}) | ||
}) | ||
|
@@ -78,7 +112,7 @@ function apiRequest(url) { | |
}) | ||
} | ||
|
||
function requestCoordsFromIP(ipAddr) { | ||
function geolocateFromIP(ipAddr) { | ||
|
||
// Validate input and throw an error if the input string isn't a valid IP address. | ||
if (!ipAddrRegex.test(ipAddr)) { | ||
|
@@ -89,20 +123,8 @@ function requestCoordsFromIP(ipAddr) { | |
return apiRequest(url) | ||
} | ||
|
||
function requestCityFromIP(ipAddr) { | ||
|
||
// Validate input and throw an error if the input string isn't a valid IP address. | ||
if (!ipAddrRegex.test(ipAddr)) { | ||
throw new Error('Invalid IP address format') | ||
} | ||
|
||
const url = buildAPIRequest(ipAddr, 'status,country,regionName,city') | ||
return apiRequest(url) | ||
} | ||
|
||
|
||
|
||
module.exports = { | ||
requestCityFromIP, | ||
requestCoordsFromIP, | ||
geolocateFromIP, | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here are the potential issues I've identified in this patch:
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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are a few potential issues with the patch:
Overall, it would be important to consider these potential issues before merging the pull request, possibly leaving comments requesting changes or further clarification. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,7 +145,7 @@ function generateForecast(weatherData) { | |
dailyForecasts[day] = singleDayForecast | ||
}) | ||
|
||
console.log(dailyForecasts) | ||
// console.log(dailyForecasts) | ||
return dailyForecasts | ||
} | ||
|
||
|
@@ -182,8 +182,8 @@ function getWeather(lat, lon, timezone) { | |
timezone: timezone | ||
}) | ||
|
||
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('&') | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided patch primarily includes commenting out 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. |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 However, based on the diff provided, there's no change in handling when There's also no check for Remember that this review is limited to the diff provided and cannot account for issues elsewhere in the codebase. |
||
|
There was a problem hiding this comment.
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.
It seems that
requestCityFromIP
andrequestCoordsFromIP
from./src/geoipAPI.js
have been replaced by single functiongeolocateFromIP
. If those functions were doing different things, this might cause problems. Review please if thegeolocateFromIP
function fully replaces both previous methods.The
weather
API call inapi.get('/')
usescoords.lat
,coords.lon
, andcoords.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.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.Ensure correct error handling: There's mixed use of Promise
.then
and.catch
chains and try-catch blocks.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.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.