Skip to content
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

Sofiia assignment week 2 #21

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

Conversation

SoniaKol
Copy link

@SoniaKol SoniaKol commented Oct 1, 2024

No description provided.

Copy link

@dyaboykyl dyaboykyl left a comment

Choose a reason for hiding this comment

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

The code is well-written and easy to read. Well done!

);
const data = await response.json();

if (data.cod === "404") {

Choose a reason for hiding this comment

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

This isn't blocking for the review, but something you can think about - what would happen if you get a different error code from the weather service? What would happen in your code and are you ok with that?

Copy link
Author

Choose a reason for hiding this comment

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

yes makes sense. i've changed it for (data.cod !== 200). thank you!

Choose a reason for hiding this comment

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

Oh I wasn't necessarily saying you should change it, just was pointing out that situation is something to think about :)

Comment on lines 26 to 28
if (data.cod !== 200) {
return res.status(data.cod).json({ weatherText: "City is not found!" });
}

Choose a reason for hiding this comment

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

I like that you're thinking about other error situations but this code actually can create a confusing scenario for the end-user. Take this example:

  1. Your API key is invalid so when your backend calls the open weather API you receive a 401 code. (You can test this by adding some extra characters to your api key)
  2. Your weather API will then send the 401 back to the user and say "City is not found!". This is potentially confusing because 1) the error message doesn't match the code (401) and 2) from the perspective of the user they did nothing wrong, so it won't be clear why they received a 401.

How might you change the code to correctly explicitly handle both a 404 as well as any other error?

Copy link
Author

@SoniaKol SoniaKol Oct 4, 2024

Choose a reason for hiding this comment

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

Yes, I completely agree it would be weird to receive a message "City is not found" with error 401. So, I've changed it with a 'double' condition:

if (data.cod !== 200) { if (data.cod === "404") { return res.status(data.cod).json({ weatherText: "City is not found!" }); } return res.status(data.cod).json({ weatherText:${data.message}}); }

So now, if we have an error, we will see the correct message about this specific error.

Choose a reason for hiding this comment

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

The double condition is a good way to go, and you could even consider using a switch statement to handle the different error codes. Looks good!

In a real-life situation you might want to be careful about passing error messages directly from the open weather api to your caller (i.e. weatherText: ${data.message}) because that can be potentially confusing if the caller of your API doesn't know what the backend is doing under-the-hood. You could even risk exposing something internal that you don't want to expose. What action you take for these "other" errors will depend on who your caller is, whether your API is public/private, your business use-cases, etc. Of course for this exercise we don't need to worry about all that :)

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