github.com/stffabi/git-lfs@v2.3.5-0.20180214015214-8eeaa8d88902+incompatible/lfsapi/errors.go (about)

     1  package lfsapi
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strings"
     7  
     8  	"github.com/git-lfs/git-lfs/errors"
     9  )
    10  
    11  type httpError interface {
    12  	Error() string
    13  	HTTPResponse() *http.Response
    14  }
    15  
    16  func IsHTTP(err error) (*http.Response, bool) {
    17  	if httpErr, ok := err.(httpError); ok {
    18  		return httpErr.HTTPResponse(), true
    19  	}
    20  	return nil, false
    21  }
    22  
    23  type ClientError struct {
    24  	Message          string `json:"message"`
    25  	DocumentationUrl string `json:"documentation_url,omitempty"`
    26  	RequestId        string `json:"request_id,omitempty"`
    27  	response         *http.Response
    28  }
    29  
    30  func (e *ClientError) HTTPResponse() *http.Response {
    31  	return e.response
    32  }
    33  
    34  func (e *ClientError) Error() string {
    35  	return e.Message
    36  }
    37  
    38  func (c *Client) handleResponse(res *http.Response) error {
    39  	if res.StatusCode < 400 {
    40  		return nil
    41  	}
    42  
    43  	cliErr := &ClientError{response: res}
    44  	err := DecodeJSON(res, cliErr)
    45  	if IsDecodeTypeError(err) {
    46  		err = nil
    47  	}
    48  
    49  	if err == nil {
    50  		if len(cliErr.Message) == 0 {
    51  			err = defaultError(res)
    52  		} else {
    53  			err = cliErr
    54  		}
    55  	}
    56  
    57  	if res.StatusCode == 401 {
    58  		return errors.NewAuthError(err)
    59  	}
    60  
    61  	if res.StatusCode > 499 && res.StatusCode != 501 && res.StatusCode != 507 && res.StatusCode != 509 {
    62  		return errors.NewFatalError(err)
    63  	}
    64  
    65  	return err
    66  }
    67  
    68  type statusCodeError struct {
    69  	response *http.Response
    70  }
    71  
    72  func NewStatusCodeError(res *http.Response) error {
    73  	return &statusCodeError{response: res}
    74  }
    75  
    76  func (e *statusCodeError) Error() string {
    77  	req := e.response.Request
    78  	return fmt.Sprintf("Invalid HTTP status for %s %s: %d",
    79  		req.Method,
    80  		strings.SplitN(req.URL.String(), "?", 2)[0],
    81  		e.response.StatusCode,
    82  	)
    83  }
    84  
    85  func (e *statusCodeError) HTTPResponse() *http.Response {
    86  	return e.response
    87  }
    88  
    89  var (
    90  	defaultErrors = map[int]string{
    91  		400: "Client error: %s",
    92  		401: "Authorization error: %s\nCheck that you have proper access to the repository",
    93  		403: "Authorization error: %s\nCheck that you have proper access to the repository",
    94  		404: "Repository or object not found: %s\nCheck that it exists and that you have proper access to it",
    95  		429: "Rate limit exceeded: %s",
    96  		500: "Server error: %s",
    97  		501: "Not Implemented: %s",
    98  		507: "Insufficient server storage: %s",
    99  		509: "Bandwidth limit exceeded: %s",
   100  	}
   101  )
   102  
   103  func defaultError(res *http.Response) error {
   104  	var msgFmt string
   105  
   106  	if f, ok := defaultErrors[res.StatusCode]; ok {
   107  		msgFmt = f
   108  	} else if res.StatusCode < 500 {
   109  		msgFmt = defaultErrors[400] + fmt.Sprintf(" from HTTP %d", res.StatusCode)
   110  	} else {
   111  		msgFmt = defaultErrors[500] + fmt.Sprintf(" from HTTP %d", res.StatusCode)
   112  	}
   113  
   114  	return errors.Errorf(msgFmt, res.Request.URL)
   115  }