github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/bindings/errors.go (about) 1 package bindings 2 3 import ( 4 "encoding/json" 5 "io/ioutil" 6 7 "github.com/containers/libpod/pkg/api/handlers/utils" 8 "github.com/pkg/errors" 9 ) 10 11 var ( 12 ErrNotImplemented = errors.New("function not implemented") 13 ) 14 15 func handleError(data []byte) error { 16 e := utils.ErrorModel{} 17 if err := json.Unmarshal(data, &e); err != nil { 18 return err 19 } 20 return e 21 } 22 23 func (a APIResponse) Process(unmarshalInto interface{}) error { 24 data, err := ioutil.ReadAll(a.Response.Body) 25 if err != nil { 26 return errors.Wrap(err, "unable to process API response") 27 } 28 if a.IsSuccess() || a.IsRedirection() { 29 if unmarshalInto != nil { 30 return json.Unmarshal(data, unmarshalInto) 31 } 32 return nil 33 } 34 // TODO should we add a debug here with the response code? 35 return handleError(data) 36 } 37 38 func CheckResponseCode(inError error) (int, error) { 39 e, ok := inError.(utils.ErrorModel) 40 if !ok { 41 return -1, errors.New("error is not type ErrorModel") 42 } 43 return e.Code(), nil 44 }