github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/bindings/errors.go (about)

     1  package bindings
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  
     7  	"github.com/containers/podman/v2/pkg/domain/entities"
     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 := entities.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.(entities.ErrorModel)
    40  	if !ok {
    41  		return -1, errors.New("error is not type ErrorModel")
    42  	}
    43  	return e.Code(), nil
    44  }