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

The code incorrectly interprets a 500 return by kong as the object is nil #29

Open
hapialex opened this issue Sep 21, 2018 · 0 comments

Comments

@hapialex
Copy link

hapialex commented Sep 21, 2018

I found this while working on the terraform kong plugin. In getService(), it doesn't check for the 500 error return by kong. When that happens, a nil object is returned, which incorrectly represents the state of the object. For example, if it's 40x, it returns an error which leads one to think 5xx should also be exposed to the client.

I fixed the code as follows but I didn't submit a PR due to two reasons.

  1. This is everywhere in the code and I fixed it in one place.
  2. I'm not sure my crude way of fixing it by using retries follows the intent of the original developer.

So I put the code here.

func (serviceClient *ServiceClient) getService(endpoint string) (*Service, error) {
	for count := 0; count < 5; count++ {
		r, body, errs := newGet(serviceClient.config, endpoint).End()
		if errs != nil {
			return nil, fmt.Errorf("could not get the service, error: %v", errs)
		}
		if r.StatusCode == 401 || r.StatusCode == 403 { // Return immediately if it's unauthorized
			return nil, fmt.Errorf("not authorised, message from kong: %s", body)
		}
		if r.StatusCode == 404 {
			return nil, nil
		}

		if r.StatusCode == http.StatusOK { // Return immediately if it's OK.
			service := &Service{}
			err := json.Unmarshal([]byte(body), service)
			if err != nil {
				return nil, fmt.Errorf("could not parse service get response, error: %v", err)
			}

			if service.Id == nil {
				return nil, nil
			}

			return service, nil
		}
	}
	return nil, fmt.Errorf("Unable to retrieve service after 5 retries")
}
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

No branches or pull requests

1 participant