-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
227 lines (202 loc) · 5.28 KB
/
errors.go
File metadata and controls
227 lines (202 loc) · 5.28 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package slog
import (
"fmt"
)
// ErrorType 错误类型枚举
type ErrorType int
const (
ErrorTypeInvalidInput ErrorType = iota
ErrorTypeProcessing
ErrorTypeConfiguration
ErrorTypeInitialization
ErrorTypeInternal
)
// String 返回错误类型的字符串表示
func (et ErrorType) String() string {
switch et {
case ErrorTypeInvalidInput:
return "InvalidInput"
case ErrorTypeProcessing:
return "Processing"
case ErrorTypeConfiguration:
return "Configuration"
case ErrorTypeInitialization:
return "Initialization"
case ErrorTypeInternal:
return "Internal"
default:
return "Unknown"
}
}
// SlogError 结构化错误类型
// 提供更丰富的错误上下文信息,便于调试和错误处理
type SlogError struct {
Type ErrorType
Component string
Operation string
Field string
Expected string
Actual string
Details map[string]interface{}
Cause error
}
// Error 实现error接口
func (e *SlogError) Error() string {
var msg string
switch e.Type {
case ErrorTypeInvalidInput:
if e.Field != "" {
msg = fmt.Sprintf("slog/%s: invalid input for field '%s'",
e.Component, e.Field)
if e.Expected != "" && e.Actual != "" {
msg += fmt.Sprintf(" - expected %s, got %s", e.Expected, e.Actual)
}
} else {
msg = fmt.Sprintf("slog/%s: invalid input in operation '%s'",
e.Component, e.Operation)
}
case ErrorTypeProcessing:
msg = fmt.Sprintf("slog/%s: processing failed in operation '%s'",
e.Component, e.Operation)
if e.Field != "" {
msg += fmt.Sprintf(" for field '%s'", e.Field)
}
case ErrorTypeConfiguration:
msg = fmt.Sprintf("slog/%s: configuration error", e.Component)
if e.Field != "" {
msg += fmt.Sprintf(" in field '%s'", e.Field)
}
case ErrorTypeInitialization:
msg = fmt.Sprintf("slog/%s: initialization failed", e.Component)
if e.Operation != "" {
msg += fmt.Sprintf(" during '%s'", e.Operation)
}
case ErrorTypeInternal:
msg = fmt.Sprintf("slog/%s: internal error", e.Component)
if e.Operation != "" {
msg += fmt.Sprintf(" in operation '%s'", e.Operation)
}
default:
msg = fmt.Sprintf("slog/%s: unknown error", e.Component)
}
if e.Cause != nil {
msg += fmt.Sprintf(" - %v", e.Cause)
}
return msg
}
// Unwrap 实现errors.Unwrap接口,支持错误链
func (e *SlogError) Unwrap() error {
return e.Cause
}
// WithDetails 添加详细信息
func (e *SlogError) WithDetails(key string, value interface{}) *SlogError {
if e.Details == nil {
e.Details = make(map[string]interface{})
}
e.Details[key] = value
return e
}
// GetDetails 获取详细信息
func (e *SlogError) GetDetails() map[string]interface{} {
if e.Details == nil {
return make(map[string]interface{})
}
return e.Details
}
// 错误构造函数
// NewInvalidInputError 创建输入无效错误
func NewInvalidInputError(field, expected, actual string) *SlogError {
return &SlogError{
Type: ErrorTypeInvalidInput,
Component: "core",
Field: field,
Expected: expected,
Actual: actual,
}
}
// NewProcessingError 创建处理错误
func NewProcessingError(component, operation string, cause error) *SlogError {
return &SlogError{
Type: ErrorTypeProcessing,
Component: component,
Operation: operation,
Cause: cause,
}
}
// NewConfigurationError 创建配置错误
func NewConfigurationError(component, field string, cause error) *SlogError {
return &SlogError{
Type: ErrorTypeConfiguration,
Component: component,
Field: field,
Cause: cause,
}
}
// NewInitializationError 创建初始化错误
func NewInitializationError(component, operation string, cause error) *SlogError {
return &SlogError{
Type: ErrorTypeInitialization,
Component: component,
Operation: operation,
Cause: cause,
}
}
// NewInternalError 创建内部错误
func NewInternalError(component, operation string, cause error) *SlogError {
return &SlogError{
Type: ErrorTypeInternal,
Component: component,
Operation: operation,
Cause: cause,
}
}
// 特定领域的错误构造函数
// NewDLPError 创建DLP相关错误
func NewDLPError(operation, field string, cause error) *SlogError {
return &SlogError{
Type: ErrorTypeProcessing,
Component: "dlp",
Operation: operation,
Field: field,
Cause: cause,
}
}
// NewModuleError 创建模块相关错误
func NewModuleError(moduleName, operation string, cause error) *SlogError {
return &SlogError{
Type: ErrorTypeProcessing,
Component: fmt.Sprintf("module/%s", moduleName),
Operation: operation,
Cause: cause,
}
}
// NewFormatterError 创建格式化器相关错误
func NewFormatterError(operation string, cause error) *SlogError {
return &SlogError{
Type: ErrorTypeProcessing,
Component: "formatter",
Operation: operation,
Cause: cause,
}
}
// IsErrorType 检查错误是否为指定类型
func IsErrorType(err error, errorType ErrorType) bool {
if slogErr, ok := err.(*SlogError); ok {
return slogErr.Type == errorType
}
return false
}
// GetErrorComponent 获取错误组件名称
func GetErrorComponent(err error) string {
if slogErr, ok := err.(*SlogError); ok {
return slogErr.Component
}
return ""
}
// GetErrorOperation 获取错误操作名称
func GetErrorOperation(err error) string {
if slogErr, ok := err.(*SlogError); ok {
return slogErr.Operation
}
return ""
}