cerror.go 683 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package cerror
  2. import "fmt"
  3. type Cerror interface {
  4. Error() string
  5. Code() int
  6. }
  7. type cerror struct {
  8. C int //code编号
  9. Msg string //错误消息
  10. }
  11. //构造函数
  12. func NewCerror(code int, msg string) Cerror {
  13. return &cerror{
  14. C: code,
  15. Msg: msg,
  16. }
  17. }
  18. //返回错误信息
  19. func (this *cerror) Error() string {
  20. return fmt.Sprintf("%s", this.Msg)
  21. }
  22. func (this *cerror) Code() int {
  23. return this.C
  24. }
  25. // 通用方法
  26. func NewECerror(code int) func(err error) Cerror {
  27. return func(err error) Cerror {
  28. return NewCerror(code, err.Error())
  29. }
  30. }
  31. func NewSCerror(code int) func(err string) Cerror {
  32. return func(err string) Cerror {
  33. return NewCerror(code, err)
  34. }
  35. }