go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/r2/errors.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package r2
     9  
    10  import (
    11  	"errors"
    12  	"net/http"
    13  	"net/url"
    14  )
    15  
    16  // Error Constants
    17  const (
    18  	// ErrRequestUnset is an error returned from options if they are called on a r2.Request that has not
    19  	// been created by r2.New(), and as a result the underlying request is uninitialized.
    20  	ErrRequestUnset Error = "r2; cannot modify request, underlying request unset. please use r2.New()"
    21  	// ErrInvalidTransport is an error returned from options if they are called on a request that has had
    22  	// the transport set to something other than an *http.Transport; this precludes using http.Transport
    23  	// specific options like tls.Config mutators.
    24  	ErrInvalidTransport Error = "r2; cannot modify transport, is not *http.Transport"
    25  	// ErrNoContentJSON is returns from sending requests when a no-content status is returned.
    26  	ErrNoContentJSON Error = "r2; server returned an http 204 for a request expecting json"
    27  	// ErrNoContentGob is returns from sending requests when a no-content status is returned.
    28  	ErrNoContentGob Error = "r2; server returned an http 204 for a request expecting a gob encoded response"
    29  	// ErrInvalidMethod is an error that is returned from `r2.Request.Do()` if a method
    30  	// is specified on the request that violates the valid charset for HTTP methods.
    31  	ErrInvalidMethod Error = "r2; invalid http method"
    32  	// ErrMismatchedPathParameters is an error that is returned from `OptParameterizedPath()` if
    33  	// the parameterized path string has a different number of parameters than what was passed as
    34  	// variadic arguments.
    35  	ErrMismatchedPathParameters Error = "r2; route parameters provided don't match parameters needed in path"
    36  	// ErrFormAndBodySet is an error where the caller has provided both a body and post form values.
    37  	ErrFormAndBodySet Error = "r2; post form and body are both set"
    38  )
    39  
    40  // Error is an error constant.
    41  type Error string
    42  
    43  // Error implements error.
    44  func (e Error) Error() string { return string(e) }
    45  
    46  // ErrIsTooManyRedirects returns if the error is too many redirects.
    47  func ErrIsTooManyRedirects(err error) bool {
    48  	if errors.Is(err, http.ErrUseLastResponse) {
    49  		return true
    50  	}
    51  	if typed, ok := err.(*url.Error); ok {
    52  		return errors.Is(typed.Err, http.ErrUseLastResponse)
    53  	}
    54  	return false
    55  }