GoogleCloudPlatform/microservices-demo · error

Unexpected status code: %d

Error message

Unexpected status code: %d

What it means

httpGetPackagingInfo calls an HTTP endpoint (packaging info service) and checks the response status. If the status is anything other than 200 OK, it returns fmt.Errorf("Unexpected status code: %d", resp.StatusCode). This library throws it to surface upstream HTTP failures (404, 500, 503, etc.) instead of trying to parse a non-JSON body.

Source

Thrown at src/frontend/packaging_info.go:62

}

func isPackagingServiceConfigured() bool {
	return packagingServiceUrl != ""
}

func httpGetPackagingInfo(productId string) (*PackagingInfo, error) {
	// Make the GET request
	url := packagingServiceUrl + "/" + productId
	fmt.Println("Requesting packaging info from URL: ", url)
	resp, err := http.Get(url)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	// Check the response status code
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("Unexpected status code: %d", resp.StatusCode)
	}

	// Read the JSON response body
	responseBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	// Decode the JSON response into a PackagingInfo struct
	var packagingInfo PackagingInfo
	err = json.Unmarshal(responseBody, &packagingInfo)
	if err != nil {
		return nil, err
	}

	return &packagingInfo, nil
}

View on GitHub (pinned to 72ba613a05)

Solutions

  1. Log/inspect the actual status code and response body to identify which non-200 was returned
  2. Verify the packaging info service URL/route configuration is correct
  3. Check the packaging service health — restart or scale it if returning 5xx
  4. Confirm auth/network policies allow the frontend to reach the endpoint
  5. Consider tolerating missing packaging info (log and degrade) instead of failing the whole product page

Example fix

// before
return nil, fmt.Errorf("Unexpected status code: %d", resp.StatusCode)
// after
body, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("packaging info request failed: status=%d body=%q", resp.StatusCode, string(body))
Defensive patterns

Strategy: fallback

Validate before calling

req, err := http.NewRequestWithContext(ctx, http.MethodGet, packagingInfoURL, nil)
if err != nil || packagingInfoURL == "" {
    return nil, fmt.Errorf("invalid packaging info URL: %v", err)
}

Try / catch

info, err := httpGetPackagingInfo(ctx, client, productID)
if err != nil {
    var statusErr *statusCodeError
    if errors.As(err, &statusErr) && statusErr.code >= 500 {
        info = defaultPackagingInfo // degrade gracefully
    } else {
        return err
    }
}

Prevention

When it happens

Trigger: Any non-200 HTTP response from the packaging info backend: client.Get succeeds at the transport level but the server replies with 404 (wrong path), 500 (backend error), 401/403, or 503.

Common situations: Packaging service URL misconfigured via env var; service deployed at a different route prefix; backend crash-looping returning 503; auth proxy stripping/injecting headers causing 403; product page load showing 500 to end users.

Related errors


AI-assisted analysis of GoogleCloudPlatform/microservices-demo@72ba613a05 (2026-09-02). Data as JSON: /api/errors/392ff8f6e0f25611. Report an issue: GitHub.