diff --git a/pkg/apiclient/resperr.go b/pkg/apiclient/resperr.go index ff954a736..6edd1334e 100644 --- a/pkg/apiclient/resperr.go +++ b/pkg/apiclient/resperr.go @@ -34,12 +34,18 @@ func CheckResponse(r *http.Response) error { data, err := io.ReadAll(r.Body) if err != nil || len(data) == 0 { - ret.Message = ptr.Of(fmt.Sprintf("http code %d, no error message", r.StatusCode)) + ret.Message = ptr.Of(fmt.Sprintf("http code %d, no response body", r.StatusCode)) return ret } - if err := json.Unmarshal(data, ret); err != nil { - return fmt.Errorf("http code %d, invalid body: %w", r.StatusCode, err) + switch r.StatusCode { + case 422: + ret.Message = ptr.Of(fmt.Sprintf("http code %d, invalid request: %s", r.StatusCode, string(data))) + default: + if err := json.Unmarshal(data, ret); err != nil { + ret.Message = ptr.Of(fmt.Sprintf("http code %d, invalid body: %s", r.StatusCode, string(data))) + return ret + } } return ret diff --git a/pkg/apiclient/usagemetrics.go b/pkg/apiclient/usagemetrics.go index 6649913a4..c418f4843 100644 --- a/pkg/apiclient/usagemetrics.go +++ b/pkg/apiclient/usagemetrics.go @@ -11,13 +11,12 @@ import ( type UsageMetricsService service func (s *UsageMetricsService) Add(ctx context.Context, metrics *models.AllMetrics) (interface{}, *Response, error) { - u := fmt.Sprintf("%s/usage-metrics/", s.client.URLPrefix) + u := fmt.Sprintf("%s/usage-metrics", s.client.URLPrefix) req, err := s.client.NewRequest(http.MethodPost, u, &metrics) if err != nil { return nil, nil, err } - var response interface{} resp, err := s.client.Do(ctx, req, &response) diff --git a/pkg/apiserver/apic_metrics.go b/pkg/apiserver/apic_metrics.go index ab13a69fe..6b32f74c3 100644 --- a/pkg/apiserver/apic_metrics.go +++ b/pkg/apiserver/apic_metrics.go @@ -290,7 +290,7 @@ func (a *apic) SendUsageMetrics() { case <-ticker.C: metrics, metricsId, err := a.GetUsageMetrics() if err != nil { - log.Errorf("unable to get usage metrics (%s)", err) + log.Errorf("unable to get usage metrics: %s", err) } /*jsonStr, err := json.Marshal(metrics) if err != nil { @@ -300,12 +300,12 @@ func (a *apic) SendUsageMetrics() { _, _, err = a.apiClient.UsageMetrics.Add(context.Background(), metrics) if err != nil { - log.Errorf("unable to send usage metrics (%s)", err) + log.Errorf("unable to send usage metrics: %s", err) } else { err = a.MarkUsageMetricsAsSent(metricsId) if err != nil { - log.Errorf("unable to mark usage metrics as sent (%s)", err) + log.Errorf("unable to mark usage metrics as sent: %s", err) } } }