github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/imaging/errors.go (about)

     1  package imaging
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"net/http"
     9  
    10  	"github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/errs"
    11  )
    12  
    13  type (
    14  	// Error is an Image and Video Manager error implementation
    15  	Error struct {
    16  		Type            string            `json:"type,omitempty"`
    17  		Title           string            `json:"title,omitempty"`
    18  		Detail          string            `json:"detail,omitempty"`
    19  		Instance        string            `json:"instance,omitempty"`
    20  		Status          int               `json:"status,omitempty"`
    21  		ProblemID       string            `json:"problemId,omitempty"`
    22  		RequestID       string            `json:"requestId,omitempty"`
    23  		IllegalValue    string            `json:"illegalValue,omitempty"`
    24  		ParameterName   string            `json:"parameterName,omitempty"`
    25  		ExtensionFields map[string]string `json:"extensionFields,omitempty"`
    26  		Method          string            `json:"method,omitempty"`
    27  		ServerIP        string            `json:"serverIp,omitempty"`
    28  		ClientIP        string            `json:"clientIp,omitempty"`
    29  		RequestTime     string            `json:"requestTime,omitempty"`
    30  		AuthzRealm      string            `json:"authzRealm,omitempty"`
    31  	}
    32  )
    33  
    34  // Error parses an error from the response
    35  func (i *imaging) Error(r *http.Response) error {
    36  	var e Error
    37  	var body []byte
    38  	body, err := ioutil.ReadAll(r.Body)
    39  	if err != nil {
    40  		i.Log(r.Request.Context()).Errorf("reading error response body: %s", err)
    41  		e.Status = r.StatusCode
    42  		e.Title = "Failed to read error body"
    43  		e.Detail = err.Error()
    44  		return &e
    45  	}
    46  
    47  	if err := json.Unmarshal(body, &e); err != nil {
    48  		i.Log(r.Request.Context()).Errorf("could not unmarshal API error: %s", err)
    49  		e.Title = "Failed to unmarshal error body. Image & Video Manager API failed. Check details for more information."
    50  		e.Detail = errs.UnescapeContent(string(body))
    51  		e.Status = r.StatusCode
    52  	}
    53  	return &e
    54  }
    55  
    56  func (e *Error) Error() string {
    57  	msg, err := json.MarshalIndent(e, "", "\t")
    58  	if err != nil {
    59  		return fmt.Sprintf("error marshaling API error: %s", err)
    60  	}
    61  	return fmt.Sprintf("API error: \n%s", msg)
    62  }
    63  
    64  // Is handles error comparisons
    65  func (e *Error) Is(target error) bool {
    66  	var t *Error
    67  	if !errors.As(target, &t) {
    68  		return false
    69  	}
    70  
    71  	if e == t {
    72  		return true
    73  	}
    74  
    75  	if e.Status != t.Status {
    76  		return false
    77  	}
    78  
    79  	return e.Error() == t.Error()
    80  }