errors.go 462 B

12345678910111213141516171819202122232425262728293031
  1. package errorx
  2. type ErrorCode int
  3. const (
  4. GENERAL_ERR ErrorCode = iota
  5. NOT_FOUND
  6. )
  7. var NotFoundErr = NewWithCode(NOT_FOUND, "not found")
  8. type Error struct {
  9. msg string
  10. code ErrorCode
  11. }
  12. func New(message string) *Error {
  13. return &Error{message, GENERAL_ERR}
  14. }
  15. func NewWithCode(code ErrorCode, message string) *Error {
  16. return &Error{message, code}
  17. }
  18. func (e *Error) Error() string {
  19. return e.msg
  20. }
  21. func (e *Error) Code() ErrorCode {
  22. return e.code
  23. }