Debian Codegraph @

common/types/err.go

raw symbols SCIP
1 // Copyright 2018 Google LLC
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 package types
16
17 import (
18 "errors"
19 "fmt"
20 "reflect"
21
22 "github.com/google/cel-go/common/types/ref"
23 )
24
25 // Error interface which allows types types.Err values to be treated as error values.
26 type Error interface {
27 error
28 ref.Val
29 }
30
31 // Err type which extends the built-in go error and implements ref.Val.
32 type Err struct {
33 error
34 id int64
35 }
36
37 var (
38 // ErrType singleton.
39 ErrType = NewOpaqueType("error")
40
41 // errDivideByZero is an error indicating a division by zero of an integer value.
42 errDivideByZero = errors.New("division by zero")
43 // errModulusByZero is an error indicating a modulus by zero of an integer value.
44 errModulusByZero = errors.New("modulus by zero")
45 // errIntOverflow is an error representing integer overflow.
46 errIntOverflow = errors.New("integer overflow")
47 // errUintOverflow is an error representing unsigned integer overflow.
48 errUintOverflow = errors.New("unsigned integer overflow")
49 // errDurationOverflow is an error representing duration overflow.
50 errDurationOverflow = errors.New("duration overflow")
51 // errTimestampOverflow is an error representing timestamp overflow.
52 errTimestampOverflow = errors.New("timestamp overflow")
53 celErrTimestampOverflow = &Err{error: errTimestampOverflow}
54
55 // celErrNoSuchOverload indicates that the call arguments did not match a supported method signature.
56 celErrNoSuchOverload = NewErr("no such overload")
57 )
58
59 // NewErr creates a new Err described by the format string and args.
60 // TODO: Audit the use of this function and standardize the error messages and codes.
61 func NewErr(format string, args ...any) ref.Val {
62 return &Err{error: fmt.Errorf(format, args...)}
63 }
64
65 // NewErrFromString creates a new Err with the provided message.
66 // TODO: Audit the use of this function and standardize the error messages and codes.
67 func NewErrFromString(message string) ref.Val {
68 return &Err{error: errors.New(message)}
69 }
70
71 // NewErrWithNodeID creates a new Err described by the format string and args.
72 // TODO: Audit the use of this function and standardize the error messages and codes.
73 func NewErrWithNodeID(id int64, format string, args ...any) ref.Val {
74 return &Err{error: fmt.Errorf(format, args...), id: id}
75 }
76
77 // LabelErrNode returns val unaltered it is not an Err or if the error has a non-zero
78 // AST node ID already present. Otherwise the id is added to the error for
79 // recovery with the Err.NodeID method.
80 func LabelErrNode(id int64, val ref.Val) ref.Val {
81 if err, ok := val.(*Err); ok && err.id == 0 {
82 err.id = id
83 return err
84 }
85 return val
86 }
87
88 // NoSuchOverloadErr returns a new types.Err instance with a no such overload message.
89 func NoSuchOverloadErr() ref.Val {
90 return celErrNoSuchOverload
91 }
92
93 // UnsupportedRefValConversionErr returns a types.NewErr instance with a no such conversion
94 // message that indicates that the native value could not be converted to a CEL ref.Val.
95 func UnsupportedRefValConversionErr(val any) ref.Val {
96 return NewErr("unsupported conversion to ref.Val: (%T)%v", val, val)
97 }
98
99 // MaybeNoSuchOverloadErr returns the error or unknown if the input ref.Val is one of these types,
100 // else a new no such overload error.
101 func MaybeNoSuchOverloadErr(val ref.Val) ref.Val {
102 return ValOrErr(val, "no such overload")
103 }
104
105 // ValOrErr either returns the existing error or creates a new one.
106 // TODO: Audit the use of this function and standardize the error messages and codes.
107 func ValOrErr(val ref.Val, format string, args ...any) ref.Val {
108 if val == nil || !IsUnknownOrError(val) {
109 return NewErr(format, args...)
110 }
111 return val
112 }
113
114 // WrapErr wraps an existing Go error value into a CEL Err value.
115 func WrapErr(err error) ref.Val {
116 return &Err{error: err}
117 }
118
119 // ConvertToNative implements ref.Val.ConvertToNative.
120 func (e *Err) ConvertToNative(typeDesc reflect.Type) (any, error) {
121 return nil, e.error
122 }
123
124 // ConvertToType implements ref.Val.ConvertToType.
125 func (e *Err) ConvertToType(typeVal ref.Type) ref.Val {
126 // Errors are not convertible to other representations.
127 return e
128 }
129
130 // Equal implements ref.Val.Equal.
131 func (e *Err) Equal(other ref.Val) ref.Val {
132 // An error cannot be equal to any other value, so it returns itself.
133 return e
134 }
135
136 // String implements fmt.Stringer.
137 func (e *Err) String() string {
138 return e.error.Error()
139 }
140
141 // Type implements ref.Val.Type.
142 func (e *Err) Type() ref.Type {
143 return ErrType
144 }
145
146 // Value implements ref.Val.Value.
147 func (e *Err) Value() any {
148 return e.error
149 }
150
151 // NodeID returns the AST node ID of the expression that returned the error.
152 func (e *Err) NodeID() int64 {
153 return e.id
154 }
155
156 // Is implements errors.Is.
157 func (e *Err) Is(target error) bool {
158 return e.error.Error() == target.Error()
159 }
160
161 // Unwrap implements errors.Unwrap.
162 func (e *Err) Unwrap() error {
163 return e.error
164 }
165
166 // IsError returns whether the input element ref.Type or ref.Val is equal to
167 // the ErrType singleton.
168 func IsError(val ref.Val) bool {
169 switch val.(type) {
170 case *Err:
171 return true
172 default:
173 return false
174 }
175 }