github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/errors/http_error.go (about)

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