github.com/m3db/m3@v1.5.0/src/dbnode/network/server/tchannelthrift/errors/errors.go (about) 1 // Copyright (c) 2016 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 "fmt" 25 26 "github.com/m3db/m3/src/dbnode/generated/thrift/rpc" 27 ) 28 29 func newError(errType rpc.ErrorType, err error, flags int64) *rpc.Error { 30 rpcErr := rpc.NewError() 31 rpcErr.Type = errType 32 rpcErr.Message = fmt.Sprintf("%v", err) 33 rpcErr.Flags = flags 34 return rpcErr 35 } 36 37 // IsInternalError returns whether the error is an internal error 38 func IsInternalError(err *rpc.Error) bool { 39 return err != nil && err.Type == rpc.ErrorType_INTERNAL_ERROR 40 } 41 42 // IsBadRequestError returns whether the error is a bad request error 43 func IsBadRequestError(err *rpc.Error) bool { 44 return err != nil && err.Type == rpc.ErrorType_BAD_REQUEST 45 } 46 47 // IsResourceExhaustedErrorFlag returns whether error has resource exhausted flag. 48 func IsResourceExhaustedErrorFlag(err *rpc.Error) bool { 49 return err != nil && err.Flags&int64(rpc.ErrorFlags_RESOURCE_EXHAUSTED) != 0 50 } 51 52 // IsTimeoutError returns whether the error is an internal error due to a timeout. 53 func IsTimeoutError(err *rpc.Error) bool { 54 return err != nil && err.Flags&int64(rpc.ErrorFlags_SERVER_TIMEOUT) != 0 55 } 56 57 // NewInternalError creates a new internal error 58 func NewInternalError(err error) *rpc.Error { 59 return newError(rpc.ErrorType_INTERNAL_ERROR, err, int64(rpc.ErrorFlags_NONE)) 60 } 61 62 // NewBadRequestError creates a new bad request error 63 func NewBadRequestError(err error) *rpc.Error { 64 return newError(rpc.ErrorType_BAD_REQUEST, err, int64(rpc.ErrorFlags_NONE)) 65 } 66 67 // NewResourceExhaustedError creates a new resource exhausted error. 68 func NewResourceExhaustedError(err error) *rpc.Error { 69 return newError(rpc.ErrorType_BAD_REQUEST, err, int64(rpc.ErrorFlags_RESOURCE_EXHAUSTED)) 70 } 71 72 // NewTimeoutError creates a new timeout error. 73 func NewTimeoutError(err error) *rpc.Error { 74 return newError(rpc.ErrorType_INTERNAL_ERROR, err, int64(rpc.ErrorFlags_SERVER_TIMEOUT)) 75 } 76 77 // NewWriteBatchRawError creates a new write batch error 78 func NewWriteBatchRawError(index int, err error) *rpc.WriteBatchRawError { 79 batchErr := rpc.NewWriteBatchRawError() 80 batchErr.Index = int64(index) 81 batchErr.Err = NewInternalError(err) 82 return batchErr 83 } 84 85 // NewBadRequestWriteBatchRawError creates a new bad request write batch error 86 func NewBadRequestWriteBatchRawError(index int, err error) *rpc.WriteBatchRawError { 87 batchErr := rpc.NewWriteBatchRawError() 88 batchErr.Index = int64(index) 89 batchErr.Err = NewBadRequestError(err) 90 return batchErr 91 }