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

enhanced error handling for open weather API errors #192

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

anveshthakur
Copy link

@anveshthakur anveshthakur commented Sep 28, 2024

Motivation and Context

This change was requested in this issue #176

Description

I've handled error and success case that is coming from open weather API where I am checking the response code from the API and then printing it to the terminal, The marshalling was not happening because API sends Code as a string value for some of cases but if there is an invalid API key they send Code value is coming as an integer value I've handled this and just checking the type and marshalling it to a new struct called openWeatherErrorResponse

Steps for Testing

To test we can check different API responses like -

  1. Invalidate API key
  2. Invalidate the open weather API
  3. For success case everything should work like it used be.

Screenshots

Invalid API key

image

Invalid API route

image

Success

image

@anveshthakur
Copy link
Author

anveshthakur commented Sep 28, 2024

Hi @kordianbruck, Let me know if any changes are required required. Thanks!

@@ -55,6 +57,15 @@ type dataBlock struct {
} `json:"rain"`
}

type openWeatherErrorReponse struct {
Cod any `json:"cod"`
Copy link
Collaborator

Choose a reason for hiding this comment

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

Lets call this Code internally, even if the API spec we parse is just cod.

Copy link
Author

Choose a reason for hiding this comment

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

I have changed Cod to Code everywhere for openWeatherErrorResponse

if res.StatusCode != 200 {
err = openWeatherErrorReponseHandler(body, url)
return nil, err
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

No need for this else, because you have a return above.

Copy link
Author

Choose a reason for hiding this comment

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

I have removed the else block and handled the API response outside of the else block

}
return &resp, nil

switch v := raw.Cod.(type) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is weird: can the type really be a string? Codes are usually only integers, so using a float64 here makes no sense to me. According to the docs this should always be an integer: https://openweathermap.org/api/one-call-3#errorstructure

Copy link
Author

@anveshthakur anveshthakur Oct 6, 2024

Choose a reason for hiding this comment

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

String and Integer Cod Value -

so, if you see this

They are sending a string in few cases like this 404 and 200 but for 400 it is an integer

Copy link
Author

Choose a reason for hiding this comment

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

Why Float64?

The reason why I am comparing type with float64 is because I am taking Code as a type of interface and when go unmarshals an interface value for numbers it returns the number with the type of float64

Source:
https://stackoverflow.com/questions/39152481/unmarshaling-a-json-integer-to-an-empty-interface-results-in-wrong-type-assertio

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

Successfully merging this pull request may close these issues.

2 participants