handle 422 error from CAPI
This commit is contained in:
parent
2b940a45f8
commit
c6ebd7ae04
3 changed files with 13 additions and 8 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue