-
Notifications
You must be signed in to change notification settings - Fork 0
FIX-3: handle bad responses from remote APIs #8
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
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 |
---|---|---|
|
@@ -176,10 +176,13 @@ function getWeather(lat, lon, timezone) { | |
return new Promise((resolve, reject) => { | ||
let weatherResponse = {} | ||
const weatherReq = http.get(url, (res) => { | ||
// console.log(`STATUS: ${res.statusCode}`) | ||
// console.log(`HEADERS: ${JSON.stringify(res.headers)}`) | ||
res.setEncoding('utf8') | ||
|
||
|
||
if (res.statusCode !== 200) { | ||
reject(new Error(`HTTP ${res.statusCode} ${res.statusMessage}`)) | ||
} | ||
|
||
let responseData = '' | ||
res.on('data', (chunk) => { | ||
// console.log(`BODY: ${JSON.stringify(chunk)}`) | ||
|
@@ -189,6 +192,11 @@ function getWeather(lat, lon, timezone) { | |
res.on('end', () => { | ||
try { | ||
weatherResponse = JSON.parse(responseData) | ||
console.log("Weather API raw response: ", weatherResponse) | ||
|
||
// TODO: Throw an error if the `weatherResponse` object | ||
// is missing the expected keys (i.e. `current`, `daily`) | ||
|
||
weatherResponse.current.weather = weatherCodes[weatherResponse.current['weather_code']] | ||
weatherResponse.daily.weather = [] | ||
weatherResponse.daily.weather_code.forEach(day => { | ||
|
@@ -209,7 +217,7 @@ function getWeather(lat, lon, timezone) { | |
}) | ||
|
||
weatherReq.on('error', (e) => { | ||
console.error(`problem with request: ${e.message}`) | ||
console.error(`Problem retrieving weather: ${e.message}`) | ||
reject(e) | ||
}) | ||
|
||
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. This patch does introduce a number of changes, and while some are certainly improvements, a few potential issues can be spotted:
Please note that some of these points may be false alarms if they are, in fact, handled elsewhere in the full codebase. Nevertheless, it's good practice to ensure such potential problems are addressed right away to maintain clean and reliable code. |
||
|
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.
The provided patch appears to contain some potential issues:
reject
is used but it's unclear where and how this reject function is defined or what defaultCoords is. In the context of promise handlers, typicallyresolve
andreject
are declared as arguments to a Promise executor function, which isn't shown here. If 'reject' and 'defaultCoords' are not properly defined elsewhere in your program, or if this code block isn't contained within a new Promise, referencing them will likely cause a ReferenceError.Verifying HTTP response status code as exactly 200 might be too strict. Other codes in the 2xx range indicate success as well. Therefore, it may break perfectly legitimate responses from the server.
The
res.on('data')
event handler may continue to receive data chunks and append them toresponseData
even after an error status code has been received and handled. This can lead to unnecessary processing/work. You might want to abort or "close" the response after detecting a non-success status code.Without more code in this vicinity (especially regarding how
apiRequest
is defined), I can't reliably comment on other potential issues.