Compare commits
No commits in common. "255d79affbf432e062f768f5700df1776be6a73c" and "be3556e23efc0517d5f47b85314522398ae2a2e2" have entirely different histories.
255d79affb
...
be3556e23e
@ -25,6 +25,4 @@ type IException interface {
|
|||||||
ToError() error
|
ToError() error
|
||||||
// IsCode 是否为指定code
|
// IsCode 是否为指定code
|
||||||
IsCode(code any) bool
|
IsCode(code any) bool
|
||||||
// GetStack 获取调用堆栈
|
|
||||||
GetStack() string
|
|
||||||
}
|
}
|
||||||
|
2
code.go
2
code.go
@ -82,7 +82,7 @@ var (
|
|||||||
// messageWithCode 自动在message文案后追加状态码
|
// messageWithCode 自动在message文案后追加状态码
|
||||||
messageWithCode = true
|
messageWithCode = true
|
||||||
// defaultSuccessCode 默认代表成功的状态码
|
// defaultSuccessCode 默认代表成功的状态码
|
||||||
defaultSuccessCode any = 0
|
defaultSuccessCode any
|
||||||
// defaultLanguage 默认的语言
|
// defaultLanguage 默认的语言
|
||||||
defaultLanguage = "zh"
|
defaultLanguage = "zh"
|
||||||
)
|
)
|
||||||
|
27
exception.go
27
exception.go
@ -10,9 +10,6 @@ package exception
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Exception 异常接口的具体实现
|
// Exception 异常接口的具体实现
|
||||||
@ -24,17 +21,12 @@ type Exception struct {
|
|||||||
code any
|
code any
|
||||||
message string
|
message string
|
||||||
data map[string]any
|
data map[string]any
|
||||||
stack string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exception) Error() string {
|
func (e *Exception) Error() string {
|
||||||
return e.Message()
|
return e.Message()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exception) GetStack() string {
|
|
||||||
return e.stack
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Exception) Code() any {
|
func (e *Exception) Code() any {
|
||||||
return e.code
|
return e.code
|
||||||
}
|
}
|
||||||
@ -91,29 +83,10 @@ func New(code any, data map[string]any, defaultMessage ...string) IException {
|
|||||||
// 保证数据结构的一致性, 同时避免后续使用出现空指针
|
// 保证数据结构的一致性, 同时避免后续使用出现空指针
|
||||||
data = map[string]any{}
|
data = map[string]any{}
|
||||||
}
|
}
|
||||||
pcs := make([]uintptr, 128)
|
|
||||||
n := runtime.Callers(2, pcs)
|
|
||||||
frames := runtime.CallersFrames(pcs[:n])
|
|
||||||
var sb strings.Builder
|
|
||||||
for {
|
|
||||||
frame, more := frames.Next()
|
|
||||||
sb.WriteString(frame.Function)
|
|
||||||
sb.WriteByte('\n')
|
|
||||||
sb.WriteByte('\t')
|
|
||||||
sb.WriteString(frame.File)
|
|
||||||
sb.WriteByte(':')
|
|
||||||
sb.WriteString(strconv.Itoa(frame.Line))
|
|
||||||
sb.WriteByte('\n')
|
|
||||||
|
|
||||||
if !more {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return &Exception{
|
return &Exception{
|
||||||
code: code,
|
code: code,
|
||||||
message: GetMessage(code, defaultMessage...),
|
message: GetMessage(code, defaultMessage...),
|
||||||
data: data,
|
data: data,
|
||||||
stack: sb.String(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user