暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package configure
  2. import (
  3. "fmt"
  4. )
  5. // ClientError SDK客户端错误
  6. type ClientError struct {
  7. Op string // 操作名称
  8. Message string // 错误信息
  9. Err error // 底层错误
  10. }
  11. // Error 实现error接口
  12. func (e *ClientError) Error() string {
  13. if e.Err != nil {
  14. return fmt.Sprintf("configure client error [%s]: %s: %v", e.Op, e.Message, e.Err)
  15. }
  16. return fmt.Sprintf("configure client error [%s]: %s", e.Op, e.Message)
  17. }
  18. // Unwrap 实现错误解包
  19. func (e *ClientError) Unwrap() error {
  20. return e.Err
  21. }
  22. // 错误定义
  23. var (
  24. // 配置错误
  25. ErrConfigInvalidURL = &ClientError{Op: "config", Message: "invalid URL"}
  26. ErrConfigInvalidAuth = &ClientError{Op: "config", Message: "invalid authentication credentials"}
  27. ErrConfigInvalidAuthType = &ClientError{Op: "config", Message: "invalid authentication type"}
  28. // 请求错误
  29. ErrRequestFailed = &ClientError{Op: "request", Message: "HTTP request failed"}
  30. ErrRequestTimeout = &ClientError{Op: "request", Message: "request timeout"}
  31. ErrInvalidResponse = &ClientError{Op: "request", Message: "invalid response format"}
  32. // API错误
  33. ErrAPIError = &ClientError{Op: "api", Message: "API returned error"}
  34. ErrNotFound = &ClientError{Op: "api", Message: "resource not found"}
  35. ErrAlreadyExists = &ClientError{Op: "api", Message: "resource already exists"}
  36. ErrUnauthorized = &ClientError{Op: "api", Message: "unauthorized"}
  37. ErrForbidden = &ClientError{Op: "api", Message: "forbidden"}
  38. ErrValidationFailed = &ClientError{Op: "api", Message: "validation failed"}
  39. // 客户端错误
  40. ErrClientNotInitialized = &ClientError{Op: "client", Message: "client not initialized"}
  41. )
  42. // NewClientError 创建新的客户端错误
  43. func NewClientError(op, message string, err error) *ClientError {
  44. return &ClientError{
  45. Op: op,
  46. Message: message,
  47. Err: err,
  48. }
  49. }
  50. // WrapError 包装错误为客户端错误
  51. func WrapError(op, message string, err error) error {
  52. if err == nil {
  53. return nil
  54. }
  55. // 如果已经是ClientError,直接返回
  56. if ce, ok := err.(*ClientError); ok {
  57. return ce
  58. }
  59. return NewClientError(op, message, err)
  60. }