github.com/codingfuture/orig-energi3@v0.8.4/swarm/storage/feed/error.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package feed
    18  
    19  import (
    20  	"fmt"
    21  )
    22  
    23  const (
    24  	ErrInit = iota
    25  	ErrNotFound
    26  	ErrIO
    27  	ErrUnauthorized
    28  	ErrInvalidValue
    29  	ErrDataOverflow
    30  	ErrNothingToReturn
    31  	ErrCorruptData
    32  	ErrInvalidSignature
    33  	ErrNotSynced
    34  	ErrPeriodDepth
    35  	ErrCnt
    36  )
    37  
    38  // Error is a the typed error object used for Swarm feeds
    39  type Error struct {
    40  	code int
    41  	err  string
    42  }
    43  
    44  // Error implements the error interface
    45  func (e *Error) Error() string {
    46  	return e.err
    47  }
    48  
    49  // Code returns the error code
    50  // Error codes are enumerated in the error.go file within the feeds package
    51  func (e *Error) Code() int {
    52  	return e.code
    53  }
    54  
    55  // NewError creates a new Swarm feeds Error object with the specified code and custom error message
    56  func NewError(code int, s string) error {
    57  	if code < 0 || code >= ErrCnt {
    58  		panic("no such error code!")
    59  	}
    60  	r := &Error{
    61  		err: s,
    62  	}
    63  	switch code {
    64  	case ErrNotFound, ErrIO, ErrUnauthorized, ErrInvalidValue, ErrDataOverflow, ErrNothingToReturn, ErrInvalidSignature, ErrNotSynced, ErrPeriodDepth, ErrCorruptData:
    65  		r.code = code
    66  	}
    67  	return r
    68  }
    69  
    70  // NewErrorf is a convenience version of NewError that incorporates printf-style formatting
    71  func NewErrorf(code int, format string, args ...interface{}) error {
    72  	return NewError(code, fmt.Sprintf(format, args...))
    73  }