terror.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package terror
  2. const (
  3. errCodeUnauthorized = 100
  4. errCodeTokenExpired = 101
  5. errCodeNoAccessToken = 102
  6. )
  7. // Common
  8. // IError : parent error.
  9. type IError struct {
  10. code int `default:"999"`
  11. message string
  12. description string
  13. }
  14. func (err IError) Error() string { return err.message }
  15. // IsSameError : Compare internal error code if target could convert IError instance.
  16. func (err IError) IsSameError(target error) bool {
  17. if convertError, e := target.(IError); !e {
  18. return false
  19. } else {
  20. return convertError.code == err.code
  21. }
  22. }
  23. // SetDescriptor : Could be add access token description.
  24. func (err IError) SetDescriptor(desc string) IError {
  25. err.description = desc
  26. return err
  27. }
  28. //
  29. // ErrorUnAuthorized : Error when request with unauthrized token
  30. type ErrorUnAuthorized IError
  31. // NewErrorUnAuthorized : Make a error instance
  32. func NewErrorUnAuthorized() ErrorUnAuthorized {
  33. return ErrorUnAuthorized{
  34. code: errCodeUnauthorized,
  35. message: "Unauthorized!",
  36. }
  37. }
  38. // ErrorTokenExpired : Token is expired!
  39. type ErrorTokenExpired IError
  40. // NewErrorTokenExpired : Make a error instance notifing Token Expired!
  41. func NewErrorTokenExpired() IError {
  42. return IError{
  43. code: errCodeUnauthorized,
  44. message: "Expired token!",
  45. }
  46. }
  47. // ErrorNoAccessToken : No access token. But it maybe unregistered user.
  48. type ErrorNoAccessToken IError
  49. // NewErrorNoAccessToken : make a instance for NoAccessToken error.
  50. func NewErrorNoAccessToken() IError {
  51. return IError{
  52. code: errCodeNoAccessToken,
  53. message: "Invalid access token.",
  54. }
  55. }