github.com/m3db/m3@v1.5.0/src/query/errors/handler.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package errors
    22  
    23  import (
    24  	"context"
    25  	"errors"
    26  	"net/http"
    27  
    28  	"github.com/m3db/m3/src/dbnode/client"
    29  	xerrors "github.com/m3db/m3/src/x/errors"
    30  	xhttp "github.com/m3db/m3/src/x/net/http"
    31  )
    32  
    33  var (
    34  	// ErrNotFound is returned when something is not found, this might be used for direct comparison
    35  	ErrNotFound = xhttp.NewError(xerrors.NewInvalidParamsError(errors.New("not found")), http.StatusNotFound)
    36  	// ErrHeaderNotFound is returned when a header is not found
    37  	ErrHeaderNotFound = xerrors.NewInvalidParamsError(errors.New("header not found"))
    38  	// ErrBatchQuery is returned when a batch query is found
    39  	ErrBatchQuery = xerrors.NewInvalidParamsError(errors.New("batch queries are currently not supported"))
    40  	// ErrNoQueryFound is returned when a target is not found
    41  	ErrNoQueryFound = xerrors.NewInvalidParamsError(errors.New("no query found"))
    42  	// ErrInvalidResultParamError is returned when result field for complete tag request
    43  	// is an unexpected value
    44  	ErrInvalidResultParamError = xerrors.NewInvalidParamsError(errors.New("invalid 'result' type for complete tag request"))
    45  	// ErrNoName is returned when no name param is provided in the resource path
    46  	ErrNoName = xerrors.NewInvalidParamsError(errors.New("invalid path with no name present"))
    47  	// ErrInvalidMatchers is returned when invalid matchers are provided
    48  	ErrInvalidMatchers = xerrors.NewInvalidParamsError(errors.New("invalid matchers"))
    49  	// ErrNamesOnly is returned when label values results are name only
    50  	ErrNamesOnly = xerrors.NewInvalidParamsError(errors.New("can not render label values; result has label names only"))
    51  	// ErrWithNames is returned when label values results are name only
    52  	ErrWithNames = xerrors.NewInvalidParamsError(errors.New("can not render label list; result has label names and values"))
    53  	// ErrMultipleResults is returned when there are multiple label values results
    54  	ErrMultipleResults = xerrors.NewInvalidParamsError(errors.New("can not render label values; multiple results detected"))
    55  )
    56  
    57  // ErrQueryTimeout is returned when a query times out executing.
    58  type ErrQueryTimeout struct {
    59  	cause error
    60  }
    61  
    62  // Error returns the error string of the causing error.
    63  func (e *ErrQueryTimeout) Error() string {
    64  	return e.cause.Error()
    65  }
    66  
    67  // Code returns an HTTP 504.
    68  func (e *ErrQueryTimeout) Code() int {
    69  	return http.StatusGatewayTimeout
    70  }
    71  
    72  // InnerError returns the cause of the query timeout.
    73  func (e *ErrQueryTimeout) InnerError() error {
    74  	return e.cause
    75  }
    76  
    77  // NewErrQueryTimeout wraps the provided causing error as an ErrQueryTimeout.
    78  func NewErrQueryTimeout(err error) *ErrQueryTimeout {
    79  	if err == nil {
    80  		return nil
    81  	}
    82  	return &ErrQueryTimeout{cause: err}
    83  }
    84  
    85  // IsTimeout returns true if the error was caused by a timeout.
    86  func IsTimeout(err error) bool {
    87  	return errors.Is(err, context.DeadlineExceeded) || client.IsTimeoutError(err)
    88  }