messages.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. This file is part of go-ethereum
  3. go-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. go-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. package rpc
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. )
  19. type InvalidTypeError struct {
  20. method string
  21. msg string
  22. }
  23. func (e *InvalidTypeError) Error() string {
  24. return fmt.Sprintf("invalid type on field %s: %s", e.method, e.msg)
  25. }
  26. func NewInvalidTypeError(method, msg string) *InvalidTypeError {
  27. return &InvalidTypeError{
  28. method: method,
  29. msg: msg,
  30. }
  31. }
  32. type InsufficientParamsError struct {
  33. have int
  34. want int
  35. }
  36. func (e *InsufficientParamsError) Error() string {
  37. return fmt.Sprintf("insufficient params, want %d have %d", e.want, e.have)
  38. }
  39. func NewInsufficientParamsError(have int, want int) *InsufficientParamsError {
  40. return &InsufficientParamsError{
  41. have: have,
  42. want: want,
  43. }
  44. }
  45. type NotImplementedError struct {
  46. Method string
  47. }
  48. func (e *NotImplementedError) Error() string {
  49. return fmt.Sprintf("%s method not implemented", e.Method)
  50. }
  51. func NewNotImplementedError(method string) *NotImplementedError {
  52. return &NotImplementedError{
  53. Method: method,
  54. }
  55. }
  56. type DecodeParamError struct {
  57. err string
  58. }
  59. func (e *DecodeParamError) Error() string {
  60. return fmt.Sprintf("could not decode, %s", e.err)
  61. }
  62. func NewDecodeParamError(errstr string) error {
  63. return &DecodeParamError{
  64. err: errstr,
  65. }
  66. }
  67. type ValidationError struct {
  68. ParamName string
  69. msg string
  70. }
  71. func (e *ValidationError) Error() string {
  72. return fmt.Sprintf("%s not valid, %s", e.ParamName, e.msg)
  73. }
  74. func NewValidationError(param string, msg string) error {
  75. return &ValidationError{
  76. ParamName: param,
  77. msg: msg,
  78. }
  79. }
  80. type RpcRequest struct {
  81. Id interface{} `json:"id"`
  82. Jsonrpc string `json:"jsonrpc"`
  83. Method string `json:"method"`
  84. Params json.RawMessage `json:"params"`
  85. }
  86. type RpcSuccessResponse struct {
  87. Id interface{} `json:"id"`
  88. Jsonrpc string `json:"jsonrpc"`
  89. Result interface{} `json:"result"`
  90. }
  91. type RpcErrorResponse struct {
  92. Id interface{} `json:"id"`
  93. Jsonrpc string `json:"jsonrpc"`
  94. Error *RpcErrorObject `json:"error"`
  95. }
  96. type RpcErrorObject struct {
  97. Code int `json:"code"`
  98. Message string `json:"message"`
  99. // Data interface{} `json:"data"`
  100. }