errors.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package errs
  17. import (
  18. "fmt"
  19. "github.com/ethereum/go-ethereum/logger"
  20. "github.com/ethereum/go-ethereum/logger/glog"
  21. )
  22. /*
  23. Errors implements an error handler providing standardised errors for a package.
  24. Fields:
  25. Errors:
  26. a map from error codes to description
  27. Package:
  28. name of the package/component
  29. Level:
  30. a function mapping error code to logger.LogLevel (severity)
  31. if not given, errors default to logger.InfoLevel
  32. */
  33. type Errors struct {
  34. Errors map[int]string
  35. Package string
  36. Level func(code int) logger.LogLevel
  37. }
  38. /*
  39. Error implements the standard go error interface.
  40. errors.New(code, format, params ...interface{})
  41. Prints as:
  42. [package] description: details
  43. where details is fmt.Sprintf(self.format, self.params...)
  44. */
  45. type Error struct {
  46. Code int
  47. Name string
  48. Package string
  49. level logger.LogLevel
  50. message string
  51. format string
  52. params []interface{}
  53. }
  54. func (self *Errors) New(code int, format string, params ...interface{}) *Error {
  55. name, ok := self.Errors[code]
  56. if !ok {
  57. panic("invalid error code")
  58. }
  59. level := logger.InfoLevel
  60. if self.Level != nil {
  61. level = self.Level(code)
  62. }
  63. return &Error{
  64. Code: code,
  65. Name: name,
  66. Package: self.Package,
  67. level: level,
  68. format: format,
  69. params: params,
  70. }
  71. }
  72. func (self Error) Error() (message string) {
  73. if len(message) == 0 {
  74. self.message = fmt.Sprintf("[%s] ERROR: %s", self.Package, self.Name)
  75. if self.format != "" {
  76. self.message += ": " + fmt.Sprintf(self.format, self.params...)
  77. }
  78. }
  79. return self.message
  80. }
  81. func (self Error) Log(v glog.Verbose) {
  82. if v {
  83. v.Infoln(self)
  84. }
  85. }
  86. /*
  87. err.Fatal() is true if err's severity level is 0 or 1 (logger.ErrorLevel or logger.Silence)
  88. */
  89. func (self *Error) Fatal() (fatal bool) {
  90. if self.level < logger.WarnLevel {
  91. fatal = true
  92. }
  93. return
  94. }