github.com/aiven/aiven-go-client@v1.36.0/error.go (about)

     1  package aiven
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // Error represents an Aiven API Error.
     9  type Error struct {
    10  	Message  string `json:"message"`
    11  	MoreInfo string `json:"more_info"`
    12  	Status   int    `json:"status"`
    13  }
    14  
    15  // Error concatenates the Status, Message and MoreInfo values.
    16  func (e Error) Error() string {
    17  	return fmt.Sprintf("%d: %s - %s", e.Status, e.Message, e.MoreInfo)
    18  }
    19  
    20  // IsNotFound returns true if the specified error has status 404
    21  func IsNotFound(err error) bool {
    22  	if e, ok := err.(Error); ok && e.Status == 404 {
    23  		return true
    24  	}
    25  
    26  	return false
    27  }
    28  
    29  // IsAlreadyExists returns true if the error message and error code that indicates that entity already exists
    30  func IsAlreadyExists(err error) bool {
    31  	if e, ok := err.(Error); ok {
    32  		if strings.Contains(e.Message, "already exists") && e.Status == 409 {
    33  			return true
    34  		}
    35  	}
    36  
    37  	return false
    38  }