github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/cloudflare/cfssl/errors/http.go (about)

     1  package errors
     2  
     3  import (
     4  	"errors"
     5  	"github.com/hellobchain/newcryptosm/http"
     6  )
     7  
     8  // HTTPError is an augmented error with a HTTP status code.
     9  type HTTPError struct {
    10  	StatusCode int
    11  	error
    12  }
    13  
    14  // Error implements the error interface.
    15  func (e *HTTPError) Error() string {
    16  	return e.error.Error()
    17  }
    18  
    19  // NewMethodNotAllowed returns an appropriate error in the case that
    20  // an HTTP client uses an invalid method (i.e. a GET in place of a POST)
    21  // on an API endpoint.
    22  func NewMethodNotAllowed(method string) *HTTPError {
    23  	return &HTTPError{http.StatusMethodNotAllowed, errors.New(`Method is not allowed:"` + method + `"`)}
    24  }
    25  
    26  // NewBadRequest creates a HttpError with the given error and error code 400.
    27  func NewBadRequest(err error) *HTTPError {
    28  	return &HTTPError{http.StatusBadRequest, err}
    29  }
    30  
    31  // NewBadRequestString returns a HttpError with the supplied message
    32  // and error code 400.
    33  func NewBadRequestString(s string) *HTTPError {
    34  	return NewBadRequest(errors.New(s))
    35  }
    36  
    37  // NewBadRequestMissingParameter returns a 400 HttpError as a required
    38  // parameter is missing in the HTTP request.
    39  func NewBadRequestMissingParameter(s string) *HTTPError {
    40  	return NewBadRequestString(`Missing parameter "` + s + `"`)
    41  }
    42  
    43  // NewBadRequestUnwantedParameter returns a 400 HttpError as a unnecessary
    44  // parameter is present in the HTTP request.
    45  func NewBadRequestUnwantedParameter(s string) *HTTPError {
    46  	return NewBadRequestString(`Unwanted parameter "` + s + `"`)
    47  }