-
Notifications
You must be signed in to change notification settings - Fork 6
Assignments week 2 Nikita #12
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
base: main
Are you sure you want to change the base?
Conversation
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.
Great Attempt, I'm very happy with the result, and good job at trying out mocking.
My only main point is to look into how you can do better error handling and error responses to make it clear to users of your server where the error came from.
|
||
describe('POST /', () => { | ||
it('returns temperature for a valid city', async () => { | ||
const fakeGeoData = [{ lat: 52.37, lon: 4.89 }] |
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.
Good use of mocking systems!
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.
Thanks a lot! :)
try { | ||
const cityName = req.body.cityName | ||
const geoData = await fetch( | ||
`http://api.openweathermap.org/geo/1.0/direct?q=${cityName}&limit=1&appid=${API_KEY}` |
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.
Whenever you have to write domains multiple times, it's good to turn them into variables/constants so they can be switched out, and so you can make sure theirs no typos in the project. e.g.
openWeathMapApi = "http://api.openweathermap.org"
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.
Good point!
const geoResponse = await geoData.json() | ||
console.log(geoResponse) | ||
if (geoResponse.length == 0) { | ||
throw new Error('City is not found') |
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.
It is best not to use errors in "normal" functionality (a user typing a city that doesn't exist is usual behaviour). it is best to just write here:
res.status(404);
res.send({ weatherText: "City is not found!" });
return;
as this would also allow us to set specific messages/status based on the errors.
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.
Got it! Makes sense
I’ll change it
temperature: (weatherResponse.main.temp - 273.15).toFixed(1) | ||
}); | ||
} catch (err) { | ||
res.json(err.message) |
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.
Best to null check this, as in theory you can "throw" anything, (even though it should be an Error by convention).
e.g the line "throw 0;" is valid, even if silly/should not be used.
Therfore, we should update this to res.json(err?.message ?? 'unknownError'). incase of null/undefined messages.
Also, when we return an error, it's good to set a different status (i.e. not 2XX) response codes, https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status A full list can be found here, but the common ones are 404 - not found, 403 - forbidden/ not logged in, 500 - server error, so 500 is a good catch all for unexpected errors like this.
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.
Thnx, Edward! I’ll add the null check and set the status to 500 for unknown errors
@@ -0,0 +1 @@ | |||
export const API_KEY = '7d47d5f41dcdf3b026655cb15bf3ff0a' |
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.
It's good practice to not push up API keys to repo's to keep them safe. two ways to do this are:
-> Ignoring this file in the .gitignore.
-> adding a .env file (and ignoring it) - This is better, as the code will still compile, and the .env file is a standard across a lot of software/programs, so loading them into the program at runtime is handleded well.
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.
You're totally right — I’ll move the key to a .env file and add it to .gitignore.
even github itself was not satisfied with my pullrequest -_-
No description provided.