messages.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 RpcConfig struct {
  20. ListenAddress string
  21. ListenPort uint
  22. CorsDomain string
  23. }
  24. type InvalidTypeError struct {
  25. method string
  26. msg string
  27. }
  28. func (e *InvalidTypeError) Error() string {
  29. return fmt.Sprintf("invalid type on field %s: %s", e.method, e.msg)
  30. }
  31. func NewInvalidTypeError(method, msg string) *InvalidTypeError {
  32. return &InvalidTypeError{
  33. method: method,
  34. msg: msg,
  35. }
  36. }
  37. type InsufficientParamsError struct {
  38. have int
  39. want int
  40. }
  41. func (e *InsufficientParamsError) Error() string {
  42. return fmt.Sprintf("insufficient params, want %d have %d", e.want, e.have)
  43. }
  44. func NewInsufficientParamsError(have int, want int) *InsufficientParamsError {
  45. return &InsufficientParamsError{
  46. have: have,
  47. want: want,
  48. }
  49. }
  50. type NotImplementedError struct {
  51. Method string
  52. }
  53. func (e *NotImplementedError) Error() string {
  54. return fmt.Sprintf("%s method not implemented", e.Method)
  55. }
  56. func NewNotImplementedError(method string) *NotImplementedError {
  57. return &NotImplementedError{
  58. Method: method,
  59. }
  60. }
  61. type DecodeParamError struct {
  62. err string
  63. }
  64. func (e *DecodeParamError) Error() string {
  65. return fmt.Sprintf("could not decode, %s", e.err)
  66. }
  67. func NewDecodeParamError(errstr string) error {
  68. return &DecodeParamError{
  69. err: errstr,
  70. }
  71. }
  72. type ValidationError struct {
  73. ParamName string
  74. msg string
  75. }
  76. func (e *ValidationError) Error() string {
  77. return fmt.Sprintf("%s not valid, %s", e.ParamName, e.msg)
  78. }
  79. func NewValidationError(param string, msg string) error {
  80. return &ValidationError{
  81. ParamName: param,
  82. msg: msg,
  83. }
  84. }
  85. type RpcRequest struct {
  86. Id interface{} `json:"id"`
  87. Jsonrpc string `json:"jsonrpc"`
  88. Method string `json:"method"`
  89. Params json.RawMessage `json:"params"`
  90. }
  91. type RpcSuccessResponse struct {
  92. Id interface{} `json:"id"`
  93. Jsonrpc string `json:"jsonrpc"`
  94. Result interface{} `json:"result"`
  95. }
  96. type RpcErrorResponse struct {
  97. Id interface{} `json:"id"`
  98. Jsonrpc string `json:"jsonrpc"`
  99. Error *RpcErrorObject `json:"error"`
  100. }
  101. type RpcErrorObject struct {
  102. Code int `json:"code"`
  103. Message string `json:"message"`
  104. // Data interface{} `json:"data"`
  105. }