github.com/binkynet/BinkyNet@v1.12.1-0.20240421190447-da4e34c20be0/apis/v1/errors.go (about)

     1  // Copyright 2020 Ewout Prangsma
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // Author Ewout Prangsma
    16  //
    17  
    18  package v1
    19  
    20  import (
    21  	"google.golang.org/grpc/codes"
    22  	"google.golang.org/grpc/status"
    23  )
    24  
    25  // CauseFunc specifies the prototype of a function that must return the cause
    26  // of the given error.
    27  // If there is not underlying cause, the given error itself must be retured.
    28  // If nil is passed, nil must be returned.
    29  type CauseFunc = func(error) error
    30  
    31  // Cause is the cause function used by the error helpers in this module.
    32  var Cause = func(err error) error { return err }
    33  
    34  // IsCanceled returns true if the given error signals a request that was canceled. Typically by the caller.
    35  func IsCanceled(err error) bool {
    36  	return status.Code(Cause(err)) == codes.Canceled
    37  }
    38  
    39  // Canceled creates a new error that signals a request that was canceled. Typically by the caller.
    40  func Canceled(msg string, args ...interface{}) error {
    41  	if len(args) > 0 {
    42  		return status.Errorf(codes.Canceled, msg, args...)
    43  	}
    44  	return status.Error(codes.Canceled, msg)
    45  }
    46  
    47  // IsDeadlineExceeded returns true if the given error signals a request that timed out.
    48  func IsDeadlineExceeded(err error) bool {
    49  	return status.Code(Cause(err)) == codes.DeadlineExceeded
    50  }
    51  
    52  // DeadlineExceeded creates a new error that signals a request that timed out.
    53  func DeadlineExceeded(msg string, args ...interface{}) error {
    54  	if len(args) > 0 {
    55  		return status.Errorf(codes.DeadlineExceeded, msg, args...)
    56  	}
    57  	return status.Error(codes.DeadlineExceeded, msg)
    58  }
    59  
    60  // IsInvalidArgument returns true if the given error signals a request with invalid arguments.
    61  func IsInvalidArgument(err error) bool {
    62  	return status.Code(Cause(err)) == codes.InvalidArgument
    63  }
    64  
    65  // InvalidArgument creates a new error that signals a request with invalid arguments.
    66  func InvalidArgument(msg string, args ...interface{}) error {
    67  	if len(args) > 0 {
    68  		return status.Errorf(codes.InvalidArgument, msg, args...)
    69  	}
    70  	return status.Error(codes.InvalidArgument, msg)
    71  }
    72  
    73  // IsNotFound returns true if the given error signals a request to an object that is not found.
    74  func IsNotFound(err error) bool {
    75  	return status.Code(Cause(err)) == codes.NotFound
    76  }
    77  
    78  // NotFound creates a new error that signals a request to an object that is not found.
    79  func NotFound(msg string, args ...interface{}) error {
    80  	if len(args) > 0 {
    81  		return status.Errorf(codes.NotFound, msg, args...)
    82  	}
    83  	return status.Error(codes.NotFound, msg)
    84  }
    85  
    86  // IsAlreadyExists returns true if the given error signals a request to create an object that already exists.
    87  func IsAlreadyExists(err error) bool {
    88  	return status.Code(Cause(err)) == codes.AlreadyExists
    89  }
    90  
    91  // AlreadyExists creates a new error that signals a request to create an object that already exists.
    92  func AlreadyExists(msg string, args ...interface{}) error {
    93  	if len(args) > 0 {
    94  		return status.Errorf(codes.AlreadyExists, msg, args...)
    95  	}
    96  	return status.Error(codes.AlreadyExists, msg)
    97  }
    98  
    99  // IsPermissionDenied returns true if the given error signals a request that the caller has not enough permissions for.
   100  func IsPermissionDenied(err error) bool {
   101  	return status.Code(Cause(err)) == codes.PermissionDenied
   102  }
   103  
   104  // PermissionDenied creates a new error that signals a request that the caller has not enough permissions for.
   105  func PermissionDenied(msg string, args ...interface{}) error {
   106  	if len(args) > 0 {
   107  		return status.Errorf(codes.PermissionDenied, msg, args...)
   108  	}
   109  	return status.Error(codes.PermissionDenied, msg)
   110  }
   111  
   112  // IsPreconditionFailed returns true if the given error signals a precondition of the request has failed.
   113  func IsPreconditionFailed(err error) bool {
   114  	return status.Code(Cause(err)) == codes.FailedPrecondition
   115  }
   116  
   117  // PreconditionFailed creates a new error that signals a request that a precondition of the call has failed.
   118  func PreconditionFailed(msg string, args ...interface{}) error {
   119  	if len(args) > 0 {
   120  		return status.Errorf(codes.FailedPrecondition, msg, args...)
   121  	}
   122  	return status.Error(codes.FailedPrecondition, msg)
   123  }
   124  
   125  // IsUnauthenticated returns true if the given error signals an unauthenticated request.
   126  func IsUnauthenticated(err error) bool {
   127  	return status.Code(Cause(err)) == codes.Unauthenticated
   128  }
   129  
   130  // Unauthenticated creates a new error that signals an unauthenticated request.
   131  func Unauthenticated(msg string, args ...interface{}) error {
   132  	if len(args) > 0 {
   133  		return status.Errorf(codes.Unauthenticated, msg, args...)
   134  	}
   135  	return status.Error(codes.Unauthenticated, msg)
   136  }
   137  
   138  // IsUnknown returns true if the given error signals an unknown error.
   139  func IsUnknown(err error) bool {
   140  	return status.Code(Cause(err)) == codes.Unknown
   141  }
   142  
   143  // Unknown creates a new error that signals an unknown error.
   144  func Unknown(msg string, args ...interface{}) error {
   145  	if len(args) > 0 {
   146  		return status.Errorf(codes.Unknown, msg, args...)
   147  	}
   148  	return status.Error(codes.Unknown, msg)
   149  }