handle 422 error from CAPI

This commit is contained in:
Sebastien Blot 2024-04-22 11:40:13 +02:00
parent 2b940a45f8
commit c6ebd7ae04
No known key found for this signature in database
GPG key ID: DFC2902F40449F6A
3 changed files with 13 additions and 8 deletions

View file

@ -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

View file

@ -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)

View file

@ -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)
}
}
}