github.com/sleungcy-sap/cli@v7.1.0+incompatible/cf/errors/http_error.go (about)

     1  package errors
     2  
     3  import (
     4  	"fmt"
     5  
     6  	. "code.cloudfoundry.org/cli/cf/i18n"
     7  )
     8  
     9  //go:generate counterfeiter . HTTPError
    10  
    11  type HTTPError interface {
    12  	Error() string
    13  	StatusCode() int   // actual HTTP status code
    14  	ErrorCode() string // error code returned in response body from CC or UAA
    15  }
    16  
    17  type baseHTTPError struct {
    18  	statusCode   int
    19  	apiErrorCode string
    20  	description  string
    21  }
    22  
    23  type HTTPNotFoundError struct {
    24  	baseHTTPError
    25  }
    26  
    27  func NewHTTPError(statusCode int, code string, description string) error {
    28  	err := baseHTTPError{
    29  		statusCode:   statusCode,
    30  		apiErrorCode: code,
    31  		description:  description,
    32  	}
    33  	switch statusCode {
    34  	case 404:
    35  		return &HTTPNotFoundError{err}
    36  	default:
    37  		return &err
    38  	}
    39  }
    40  
    41  func (err *baseHTTPError) StatusCode() int {
    42  	return err.statusCode
    43  }
    44  
    45  func (err *baseHTTPError) Error() string {
    46  	return fmt.Sprintf(T("Server error, status code: {{.ErrStatusCode}}, error code: {{.ErrAPIErrorCode}}, message: {{.ErrDescription}}",
    47  		map[string]interface{}{"ErrStatusCode": err.statusCode,
    48  			"ErrAPIErrorCode": err.apiErrorCode,
    49  			"ErrDescription":  err.description}),
    50  	)
    51  }
    52  
    53  func (err *baseHTTPError) ErrorCode() string {
    54  	return err.apiErrorCode
    55  }