t_error.go 532 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package errors
  2. import "fmt"
  3. type TError struct {
  4. code int
  5. msg string
  6. }
  7. // 实现 Error 接口
  8. func (e TError) Error() string {
  9. return fmt.Sprintf("code:%d,msg:%v", e.code, e.msg)
  10. }
  11. // New new Error
  12. func NewTError(code int, msg string) error {
  13. return TError{
  14. code: code,
  15. msg: msg,
  16. }
  17. }
  18. // GetCode 获取Code
  19. func GetCode(err error) int {
  20. if e, ok := err.(TError); ok {
  21. return e.code
  22. }
  23. return -1
  24. }
  25. // GetMsg 获取Msg
  26. func GetMsg(err error) string {
  27. if e, ok := err.(TError); ok {
  28. return e.msg
  29. }
  30. return ""
  31. }