-
Notifications
You must be signed in to change notification settings - Fork 3
/
context.go
95 lines (79 loc) · 2.93 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2023 Cisco Systems, Inc. and its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package errorutils
import (
"encoding/gob"
"fmt"
"reflect"
)
var reserved = map[int64]string{}
func init() {
gob.Register((*CodedError)(nil))
gob.Register((*nestedError)(nil))
}
// Reserve is used for error hierarchy defining packages to validate and reserve its error code range
// it's usually called during init()
// this funciton does following things:
// 1. validate given err implements error, ErrorCoder and ComparableErrorCoder
// 2. the mask equals ReservedMask (a category error created via NewErrorCategory)
// 3. bits lower than ReservedOffset of the given error's code are all 0
// 4. if the code is available (not registered by other packages)
// 5. try to register the error's implementation with gob
func Reserve(err interface{}) {
switch err.(type) {
case error, ErrorCoder, ComparableErrorCoder:
default:
panic(fmt.Errorf("cannot reserve error category %T", err))
}
if masker := err.(ComparableErrorCoder); masker.CodeMask() != ReservedMask {
panic(fmt.Errorf("cannot reserve error category with code mask %x", masker.CodeMask()))
}
coder := err.(ErrorCoder)
if coder.Code() & ^ReservedMask != 0 {
panic(fmt.Errorf("cannot reserve error category with code %x, it's not a category level codes", coder.Code() ))
}
if pkg, ok := reserved[coder.Code()]; ok {
panic(fmt.Errorf("error category with code %x is already registered by ", pkg ))
}
// try reserve
gob.Register(err)
reserved[coder.Code()] = reflect.TypeOf(err).PkgPath()
}
type ErrorCoder interface {
Code() int64
}
type ComparableErrorCoder interface {
CodeMask() int64
}
type NestedError interface {
// Cause returns directly nested error
Cause() error
// RootCause returns the root cause of error, equivalent to calling Cause repeatedly
RootCause() error
}
type ComparableError interface {
Is(target error) bool
}
type Unwrapper interface {
Unwrap() error
}
// Hasher is an interface for error implementations that not naturally hashable to be used as a map key
// e.g. if a struct containing Slice, Map, Array are not hashable. Therefore, use those errors as map key would panic
// For implementations that need use error as map key (e.g. map[error]interface{}) should use Hasher.Hash as key
// Note: CodedError doesn't implement this interface because itself is hashable
type Hasher interface {
Hash() error
}