github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/swarm/storage/mru/error.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:50</date> 10 //</624342682881232896> 11 12 // 13 // 14 // 15 // 16 // 17 // 18 // 19 // 20 // 21 // 22 // 23 // 24 // 25 // 26 // 27 28 package mru 29 30 import ( 31 "fmt" 32 ) 33 34 const ( 35 ErrInit = iota 36 ErrNotFound 37 ErrIO 38 ErrUnauthorized 39 ErrInvalidValue 40 ErrDataOverflow 41 ErrNothingToReturn 42 ErrCorruptData 43 ErrInvalidSignature 44 ErrNotSynced 45 ErrPeriodDepth 46 ErrCnt 47 ) 48 49 // 50 type Error struct { 51 code int 52 err string 53 } 54 55 // 56 func (e *Error) Error() string { 57 return e.err 58 } 59 60 // 61 // 62 func (e *Error) Code() int { 63 return e.code 64 } 65 66 // 67 func NewError(code int, s string) error { 68 if code < 0 || code >= ErrCnt { 69 panic("no such error code!") 70 } 71 r := &Error{ 72 err: s, 73 } 74 switch code { 75 case ErrNotFound, ErrIO, ErrUnauthorized, ErrInvalidValue, ErrDataOverflow, ErrNothingToReturn, ErrInvalidSignature, ErrNotSynced, ErrPeriodDepth, ErrCorruptData: 76 r.code = code 77 } 78 return r 79 } 80 81 // 82 func NewErrorf(code int, format string, args ...interface{}) error { 83 return NewError(code, fmt.Sprintf(format, args...)) 84 } 85