github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/swarm/storage/feed/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 19:16:44</date>
    10  //</624450118757650432>
    11  
    12  
    13  package feed
    14  
    15  import (
    16  	"fmt"
    17  )
    18  
    19  const (
    20  	ErrInit = iota
    21  	ErrNotFound
    22  	ErrIO
    23  	ErrUnauthorized
    24  	ErrInvalidValue
    25  	ErrDataOverflow
    26  	ErrNothingToReturn
    27  	ErrCorruptData
    28  	ErrInvalidSignature
    29  	ErrNotSynced
    30  	ErrPeriodDepth
    31  	ErrCnt
    32  )
    33  
    34  //错误是用于Swarm提要的类型化错误对象
    35  type Error struct {
    36  	code int
    37  	err  string
    38  }
    39  
    40  //错误实现错误接口
    41  func (e *Error) Error() string {
    42  	return e.err
    43  }
    44  
    45  //代码返回错误代码
    46  //错误代码在feeds包中的error.go文件中枚举。
    47  func (e *Error) Code() int {
    48  	return e.code
    49  }
    50  
    51  //newError用指定的代码和自定义错误消息创建一个新的swarm feeds错误对象
    52  func NewError(code int, s string) error {
    53  	if code < 0 || code >= ErrCnt {
    54  		panic("no such error code!")
    55  	}
    56  	r := &Error{
    57  		err: s,
    58  	}
    59  	switch code {
    60  	case ErrNotFound, ErrIO, ErrUnauthorized, ErrInvalidValue, ErrDataOverflow, ErrNothingToReturn, ErrInvalidSignature, ErrNotSynced, ErrPeriodDepth, ErrCorruptData:
    61  		r.code = code
    62  	}
    63  	return r
    64  }
    65  
    66  //newerrorf是newerror的一个方便版本,它包含了printf样式的格式。
    67  func NewErrorf(code int, format string, args ...interface{}) error {
    68  	return NewError(code, fmt.Sprintf(format, args...))
    69  }
    70