errors.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2015 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 rpc
  17. import "fmt"
  18. // HTTPError is returned by client operations when the HTTP status code of the
  19. // response is not a 2xx status.
  20. type HTTPError struct {
  21. StatusCode int
  22. Status string
  23. Body []byte
  24. }
  25. func (err HTTPError) Error() string {
  26. if len(err.Body) == 0 {
  27. return err.Status
  28. }
  29. return fmt.Sprintf("%v: %s", err.Status, err.Body)
  30. }
  31. // Error wraps RPC errors, which contain an error code in addition to the message.
  32. type Error interface {
  33. Error() string // returns the message
  34. ErrorCode() int // returns the code
  35. }
  36. // A DataError contains some data in addition to the error message.
  37. type DataError interface {
  38. Error() string // returns the message
  39. ErrorData() interface{} // returns the error data
  40. }
  41. // Error types defined below are the built-in JSON-RPC errors.
  42. var (
  43. _ Error = new(methodNotFoundError)
  44. _ Error = new(subscriptionNotFoundError)
  45. _ Error = new(parseError)
  46. _ Error = new(invalidRequestError)
  47. _ Error = new(invalidMessageError)
  48. _ Error = new(invalidParamsError)
  49. )
  50. const defaultErrorCode = -32000
  51. type methodNotFoundError struct{ method string }
  52. func (e *methodNotFoundError) ErrorCode() int { return -32601 }
  53. func (e *methodNotFoundError) Error() string {
  54. return fmt.Sprintf("the method %s does not exist/is not available", e.method)
  55. }
  56. type subscriptionNotFoundError struct{ namespace, subscription string }
  57. func (e *subscriptionNotFoundError) ErrorCode() int { return -32601 }
  58. func (e *subscriptionNotFoundError) Error() string {
  59. return fmt.Sprintf("no %q subscription in %s namespace", e.subscription, e.namespace)
  60. }
  61. // Invalid JSON was received by the server.
  62. type parseError struct{ message string }
  63. func (e *parseError) ErrorCode() int { return -32700 }
  64. func (e *parseError) Error() string { return e.message }
  65. // received message isn't a valid request
  66. type invalidRequestError struct{ message string }
  67. func (e *invalidRequestError) ErrorCode() int { return -32600 }
  68. func (e *invalidRequestError) Error() string { return e.message }
  69. // received message is invalid
  70. type invalidMessageError struct{ message string }
  71. func (e *invalidMessageError) ErrorCode() int { return -32700 }
  72. func (e *invalidMessageError) Error() string { return e.message }
  73. // unable to decode supplied params, or an invalid number of parameters
  74. type invalidParamsError struct{ message string }
  75. func (e *invalidParamsError) ErrorCode() int { return -32602 }
  76. func (e *invalidParamsError) Error() string { return e.message }