ecode.go 964 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package pb
  2. import (
  3. "strconv"
  4. "go-common/library/ecode"
  5. any "github.com/golang/protobuf/ptypes/any"
  6. )
  7. func (e *Error) Error() string {
  8. return strconv.FormatInt(int64(e.GetErrCode()), 10)
  9. }
  10. // Code is the code of error.
  11. func (e *Error) Code() int {
  12. return int(e.GetErrCode())
  13. }
  14. // Message is error message.
  15. func (e *Error) Message() string {
  16. return e.GetErrMessage()
  17. }
  18. // Equal compare whether two errors are equal.
  19. func (e *Error) Equal(ec error) bool {
  20. return ecode.Cause(ec).Code() == e.Code()
  21. }
  22. // Details return error details.
  23. func (e *Error) Details() []interface{} {
  24. return []interface{}{e.GetErrDetail()}
  25. }
  26. // From will convert ecode.Codes to pb.Error.
  27. //
  28. // Deprecated: please use ecode.Error
  29. func From(ec ecode.Codes) *Error {
  30. var detail *any.Any
  31. if details := ec.Details(); len(details) > 0 {
  32. detail, _ = details[0].(*any.Any)
  33. }
  34. return &Error{
  35. ErrCode: int32(ec.Code()),
  36. ErrMessage: ec.Message(),
  37. ErrDetail: detail,
  38. }
  39. }