github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/caveats/errors.go (about) 1 package caveats 2 3 import ( 4 "errors" 5 "fmt" 6 7 "github.com/rs/zerolog" 8 "google.golang.org/grpc/codes" 9 "google.golang.org/grpc/status" 10 11 v1 "github.com/authzed/authzed-go/proto/authzed/api/v1" 12 13 "github.com/authzed/spicedb/pkg/caveats" 14 core "github.com/authzed/spicedb/pkg/proto/core/v1" 15 "github.com/authzed/spicedb/pkg/spiceerrors" 16 ) 17 18 // EvaluationErr is an error in evaluation of a caveat expression. 19 type EvaluationErr struct { 20 error 21 caveatExpr *core.CaveatExpression 22 evalErr caveats.EvaluationErr 23 } 24 25 // MarshalZerologObject implements zerolog.LogObjectMarshaler 26 func (err EvaluationErr) MarshalZerologObject(e *zerolog.Event) { 27 e.Err(err.error).Str("caveat_name", err.caveatExpr.GetCaveat().CaveatName).Interface("context", err.caveatExpr.GetCaveat().Context) 28 } 29 30 // DetailsMetadata returns the metadata for details for this error. 31 func (err EvaluationErr) DetailsMetadata() map[string]string { 32 return spiceerrors.CombineMetadata(err.evalErr, map[string]string{ 33 "caveat_name": err.caveatExpr.GetCaveat().CaveatName, 34 }) 35 } 36 37 func (err EvaluationErr) GRPCStatus() *status.Status { 38 return spiceerrors.WithCodeAndDetails( 39 err, 40 codes.InvalidArgument, 41 spiceerrors.ForReason( 42 v1.ErrorReason_ERROR_REASON_CAVEAT_EVALUATION_ERROR, 43 err.DetailsMetadata(), 44 ), 45 ) 46 } 47 48 func NewEvaluationErr(caveatExpr *core.CaveatExpression, err caveats.EvaluationErr) EvaluationErr { 49 return EvaluationErr{ 50 fmt.Errorf("evaluation error for caveat %s: %w", caveatExpr.GetCaveat().CaveatName, err), caveatExpr, err, 51 } 52 } 53 54 // ParameterTypeError is a type error in constructing a parameter from a value. 55 type ParameterTypeError struct { 56 error 57 caveatExpr *core.CaveatExpression 58 conversionError *caveats.ParameterConversionErr 59 } 60 61 // MarshalZerologObject implements zerolog.LogObjectMarshaler 62 func (err ParameterTypeError) MarshalZerologObject(e *zerolog.Event) { 63 evt := e.Err(err.error). 64 Str("caveat_name", err.caveatExpr.GetCaveat().CaveatName). 65 Interface("context", err.caveatExpr.GetCaveat().Context) 66 67 if err.conversionError != nil { 68 evt.Str("parameter_name", err.conversionError.ParameterName()) 69 } 70 } 71 72 // DetailsMetadata returns the metadata for details for this error. 73 func (err ParameterTypeError) DetailsMetadata() map[string]string { 74 if err.conversionError != nil { 75 return spiceerrors.CombineMetadata(err.conversionError, map[string]string{ 76 "caveat_name": err.caveatExpr.GetCaveat().CaveatName, 77 }) 78 } 79 80 return map[string]string{ 81 "caveat_name": err.caveatExpr.GetCaveat().CaveatName, 82 } 83 } 84 85 func (err ParameterTypeError) GRPCStatus() *status.Status { 86 return spiceerrors.WithCodeAndDetails( 87 err, 88 codes.InvalidArgument, 89 spiceerrors.ForReason( 90 v1.ErrorReason_ERROR_REASON_CAVEAT_PARAMETER_TYPE_ERROR, 91 err.DetailsMetadata(), 92 ), 93 ) 94 } 95 96 func NewParameterTypeError(caveatExpr *core.CaveatExpression, err error) ParameterTypeError { 97 conversionError := &caveats.ParameterConversionErr{} 98 if !errors.As(err, conversionError) { 99 conversionError = nil 100 } 101 102 return ParameterTypeError{ 103 fmt.Errorf("type error for parameters for caveat `%s`: %w", caveatExpr.GetCaveat().CaveatName, err), 104 caveatExpr, 105 conversionError, 106 } 107 }