添加文件: errors.go

This commit is contained in:
renzhiyuan 2026-07-21 18:16:23 +08:00
parent f00a93afae
commit 84fac4ce78
1 changed files with 41 additions and 0 deletions

41
errors.go Normal file
View File

@ -0,0 +1,41 @@
package xyshanghai
import "fmt"
// ErrorCode 定义错误码类型
type ErrorCode int
const (
// ErrCodeSuccess 成功
ErrCodeSuccess ErrorCode = 0
// ErrCodeFailure 失败
ErrCodeFailure ErrorCode = -1
)
// SDKError 自定义SDK错误
type SDKError struct {
Code ErrorCode
Message string
Err error
}
func (e *SDKError) Error() string {
if e.Err != nil {
return fmt.Sprintf("code=%d, message=%s, err=%v", e.Code, e.Message, e.Err)
}
return fmt.Sprintf("code=%d, message=%s", e.Code, e.Message)
}
// Unwrap 返回内部错误
func (e *SDKError) Unwrap() error {
return e.Err
}
// NewSDKError 创建SDK错误
func NewSDKError(code ErrorCode, message string, err error) *SDKError {
return &SDKError{
Code: code,
Message: message,
Err: err,
}
}