-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.go
153 lines (120 loc) · 3.53 KB
/
code.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2021 dairongpeng <dairongpeng@foxmail.com>. All rights reserved.
//
// 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.
package errors
import (
"fmt"
"net/http"
"sync"
)
var (
unknownCoder defaultCoder = defaultCoder{1, http.StatusInternalServerError, "An internal server error occurred", "http://github.com/dairongpeng/leona/pkg/errors/README.md"}
)
// Coder defines an interface for an error code detail information.
type Coder interface {
// HTTP status that should be used for the associated error code.
HTTPStatus() int
// External (user) facing error text.
String() string
// Reference returns the detail documents for user.
Reference() string
// Code returns the code of the coder
Code() int
}
type defaultCoder struct {
// C refers to the integer code of the ErrCode.
C int
// HTTP status that should be used for the associated error code.
HTTP int
// External (user) facing error text.
Ext string
// Ref specify the reference document.
Ref string
}
// Code returns the integer code of the coder.
func (coder defaultCoder) Code() int {
return coder.C
}
// String implements stringer. String returns the external error message,
// if any.
func (coder defaultCoder) String() string {
return coder.Ext
}
// HTTPStatus returns the associated HTTP status code, if any. Otherwise,
// returns 200.
func (coder defaultCoder) HTTPStatus() int {
if coder.HTTP == 0 {
return 500
}
return coder.HTTP
}
// Reference returns the reference document.
func (coder defaultCoder) Reference() string {
return coder.Ref
}
// codes contains a map of error codes to metadata.
var codes = map[int]Coder{}
var codeMux = &sync.Mutex{}
// Register register a user define error code.
// It will overrid the exist code.
func Register(coder Coder) {
if coder.Code() == 0 {
panic("code `0` is reserved by `github.com/dairongpeng/leona/pkg/errors` as unknownCode error code")
}
codeMux.Lock()
defer codeMux.Unlock()
codes[coder.Code()] = coder
}
// MustRegister register a user define error code.
// It will panic when the same Code already exist.
func MustRegister(coder Coder) {
if coder.Code() == 0 {
panic("code '0' is reserved by 'github.com/dairongpeng/leona/pkg/errors' as ErrUnknown error code")
}
codeMux.Lock()
defer codeMux.Unlock()
if _, ok := codes[coder.Code()]; ok {
panic(fmt.Sprintf("code: %d already exist", coder.Code()))
}
codes[coder.Code()] = coder
}
// ParseCoder parse any error into *withCode.
// nil error will return nil direct.
// None withStack error will be parsed as ErrUnknown.
func ParseCoder(err error) Coder {
if err == nil {
return nil
}
if v, ok := err.(*withCode); ok {
if coder, ok := codes[v.code]; ok {
return coder
}
}
return unknownCoder
}
// IsCode reports whether any error in err's chain contains the given error code.
func IsCode(err error, code int) bool {
if v, ok := err.(*withCode); ok {
if v.code == code {
return true
}
if v.cause != nil {
return IsCode(v.cause, code)
}
return false
}
return false
}
func init() {
codes[unknownCoder.Code()] = unknownCoder
}