Skip to content

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

NStefanchuk
Copy link

No description provided.

@NStefanchuk NStefanchuk changed the title Nikita w2 node Assignments week 1 Nikita May 7, 2025
@EdwardSmart98 EdwardSmart98 changed the title Assignments week 1 Nikita Assignments week 2 Nikita May 11, 2025
Copy link

@EdwardSmart98 EdwardSmart98 left a 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 }]

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!

Copy link
Author

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}`

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"

Copy link
Author

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')

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.

Copy link
Author

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)

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.

Copy link
Author

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'

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.

Copy link
Author

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 -_-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants