github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/storage/mru/error.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 // 10 // 11 // 12 // 13 // 14 // 15 // 16 // 17 // 18 // 19 // 20 // 21 // 22 // 23 // 24 25 package mru 26 27 import ( 28 "fmt" 29 ) 30 31 const ( 32 ErrInit = iota 33 ErrNotFound 34 ErrIO 35 ErrUnauthorized 36 ErrInvalidValue 37 ErrDataOverflow 38 ErrNothingToReturn 39 ErrCorruptData 40 ErrInvalidSignature 41 ErrNotSynced 42 ErrPeriodDepth 43 ErrCnt 44 ) 45 46 // 47 type Error struct { 48 code int 49 err string 50 } 51 52 // 53 func (e *Error) Error() string { 54 return e.err 55 } 56 57 // 58 // 59 func (e *Error) Code() int { 60 return e.code 61 } 62 63 // 64 func NewError(code int, s string) error { 65 if code < 0 || code >= ErrCnt { 66 panic("no such error code!") 67 } 68 r := &Error{ 69 err: s, 70 } 71 switch code { 72 case ErrNotFound, ErrIO, ErrUnauthorized, ErrInvalidValue, ErrDataOverflow, ErrNothingToReturn, ErrInvalidSignature, ErrNotSynced, ErrPeriodDepth, ErrCorruptData: 73 r.code = code 74 } 75 return r 76 } 77 78 // 79 func NewErrorf(code int, format string, args ...interface{}) error { 80 return NewError(code, fmt.Sprintf(format, args...)) 81 }