Azure: Improve resource request error handling (#99017)

* Improve resource request error handling

- Correctly parse JSON responses
- Log erroneous failures in JSON marshalling/unmarshalling
- Correctly set response status code
- Do not attempt to use the response writer as it will be nil

* Minor change

* Improve type assertion handling
pull/99130/head
Andreas Christou 5 months ago committed by GitHub
parent b4ec11e150
commit f6194931f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 40
      pkg/tsdb/azuremonitor/azuremonitor-resource-handler.go

@ -33,30 +33,36 @@ func (s *httpServiceProxy) writeErrorResponse(rw http.ResponseWriter, statusCode
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(statusCode)
// Set error response to initial error message
errorBody := map[string]string{"error": message}
// Attempt to locate JSON portion in error message
re := regexp.MustCompile(`\{.*?\}`)
jsonPart := re.FindString(message)
if jsonPart != "" {
var jsonData map[string]interface{}
if unmarshalErr := json.Unmarshal([]byte(jsonPart), &jsonData); unmarshalErr != nil {
errorMsg, _ := json.Marshal(map[string]string{"error": "Invalid JSON format in error message"})
_, err := rw.Write(errorMsg)
if err != nil {
return fmt.Errorf("unable to write HTTP response: %v", err)
}
return unmarshalErr
}
errorBody["error"] = fmt.Sprintf("Invalid JSON format in error message. Raw error: %s", message)
s.logger.Error("failed to unmarshal JSON error message", "error", unmarshalErr)
} else {
// Extract relevant fields for a formatted error message
errorType, _ := jsonData["error"].(string)
errorDescription, _ := jsonData["error_description"].(string)
errorDescription, ok := jsonData["error_description"].(string)
if !ok {
s.logger.Error("unable to convert error_description to string", "rawError", jsonData["error_description"])
// Attempt to just format the error as a string
errorDescription = fmt.Sprintf("%v", jsonData["error_description"])
}
if errorType == "" {
errorType = "UnknownError"
}
formattedError := fmt.Sprintf("%s: %s", errorType, errorDescription)
errorMsg, _ := json.Marshal(map[string]string{"error": formattedError})
_, err := rw.Write(errorMsg)
errorBody["error"] = fmt.Sprintf("%s: %s", errorType, errorDescription)
}
}
jsonRes, _ := json.Marshal(errorBody)
_, err := rw.Write(jsonRes)
if err != nil {
return fmt.Errorf("unable to write HTTP response: %v", err)
}
@ -117,7 +123,7 @@ func (s *Service) getDataSourceFromHTTPReq(req *http.Request) (types.DatasourceI
}
func writeErrorResponse(rw http.ResponseWriter, code int, msg string) {
rw.WriteHeader(http.StatusBadRequest)
rw.WriteHeader(code)
errorBody := map[string]string{
"error": msg,
}
@ -154,9 +160,11 @@ func (s *Service) handleResourceReq(subDataSource string) func(rw http.ResponseW
req.URL.Host = serviceURL.Host
req.URL.Scheme = serviceURL.Scheme
rw, err = s.executors[subDataSource].ResourceRequest(rw, req, service.HTTPClient)
_, err = s.executors[subDataSource].ResourceRequest(rw, req, service.HTTPClient)
if err != nil {
writeErrorResponse(rw, http.StatusInternalServerError, fmt.Sprintf("unexpected error %v", err))
// The ResourceRequest function should handle writing the error response
// We log the error here to ensure it's captured
s.logger.Error("error in resource request", "error", err)
return
}
}

Loading…
Cancel
Save