error.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. package feeds
  17. import (
  18. "fmt"
  19. )
  20. const (
  21. ErrInit = iota
  22. ErrNotFound
  23. ErrIO
  24. ErrUnauthorized
  25. ErrInvalidValue
  26. ErrDataOverflow
  27. ErrNothingToReturn
  28. ErrCorruptData
  29. ErrInvalidSignature
  30. ErrNotSynced
  31. ErrPeriodDepth
  32. ErrCnt
  33. )
  34. // Error is a the typed error object used for Swarm Feeds
  35. type Error struct {
  36. code int
  37. err string
  38. }
  39. // Error implements the error interface
  40. func (e *Error) Error() string {
  41. return e.err
  42. }
  43. // Code returns the error code
  44. // Error codes are enumerated in the error.go file within the feeds package
  45. func (e *Error) Code() int {
  46. return e.code
  47. }
  48. // NewError creates a new Swarm Feeds Error object with the specified code and custom error message
  49. func NewError(code int, s string) error {
  50. if code < 0 || code >= ErrCnt {
  51. panic("no such error code!")
  52. }
  53. r := &Error{
  54. err: s,
  55. }
  56. switch code {
  57. case ErrNotFound, ErrIO, ErrUnauthorized, ErrInvalidValue, ErrDataOverflow, ErrNothingToReturn, ErrInvalidSignature, ErrNotSynced, ErrPeriodDepth, ErrCorruptData:
  58. r.code = code
  59. }
  60. return r
  61. }
  62. // NewErrorf is a convenience version of NewError that incorporates printf-style formatting
  63. func NewErrorf(code int, format string, args ...interface{}) error {
  64. return NewError(code, fmt.Sprintf(format, args...))
  65. }