From 2be0b1f979f411ce0361c2ad0acec2b705344fb0 Mon Sep 17 00:00:00 2001 From: renzhiyuan <465386466@qq.com> Date: Thu, 23 Jul 2026 18:14:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6:=20ymt=5Fv?= =?UTF-8?q?3/errors.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ymt_v3/errors.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 ymt_v3/errors.go diff --git a/ymt_v3/errors.go b/ymt_v3/errors.go new file mode 100644 index 0000000..64682d7 --- /dev/null +++ b/ymt_v3/errors.go @@ -0,0 +1,50 @@ +package ymt_v3 + +import "fmt" + +// APIError 表示API返回的业务错误 +type APIError struct { + Code int32 `json:"code"` + Message string `json:"message"` + Reason string `json:"reason,omitempty"` +} + +func (e *APIError) Error() string { + return fmt.Sprintf("API error: code=%d, message=%s, reason=%s", e.Code, e.Message, e.Reason) +} + +// IsSuccess 判断是否成功 +func (e *APIError) IsSuccess() bool { + return e.Code == 200 +} + +// NewAPIError 创建API错误 +func NewAPIError(code int32, message, reason string) *APIError { + return &APIError{ + Code: code, + Message: message, + Reason: reason, + } +} + +// SDKError SDK内部错误 +type SDKError struct { + Message string + Err error +} + +func (e *SDKError) Error() string { + if e.Err != nil { + return fmt.Sprintf("SDK error: %s: %v", e.Message, e.Err) + } + return fmt.Sprintf("SDK error: %s", e.Message) +} + +func (e *SDKError) Unwrap() error { + return e.Err +} + +// NewSDKError 创建SDK内部错误 +func NewSDKError(msg string, err error) *SDKError { + return &SDKError{Message: msg, Err: err} +} \ No newline at end of file