You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.7 KiB

  1. package jsonrpc
  2. import "fmt"
  3. const (
  4. // ErrorCodeParse is parse error code.
  5. ErrorCodeParse ErrorCode = -32700
  6. // ErrorCodeInvalidRequest is invalid request error code.
  7. ErrorCodeInvalidRequest ErrorCode = -32600
  8. // ErrorCodeMethodNotFound is method not found error code.
  9. ErrorCodeMethodNotFound ErrorCode = -32601
  10. // ErrorCodeInvalidParams is invalid params error code.
  11. ErrorCodeInvalidParams ErrorCode = -32602
  12. // ErrorCodeInternal is internal error code.
  13. ErrorCodeInternal ErrorCode = -32603
  14. )
  15. type (
  16. // A ErrorCode by JSON-RPC 2.0.
  17. ErrorCode int
  18. // An Error is a wrapper for a JSON interface value.
  19. Error struct {
  20. Code ErrorCode `json:"code"`
  21. Message string `json:"message"`
  22. Data interface{} `json:"data,omitempty"`
  23. }
  24. )
  25. // Error implements error interface.
  26. func (e *Error) Error() string {
  27. return fmt.Sprintf("jsonrpc: code: %d, message: %s, data: %+v", e.Code, e.Message, e.Data)
  28. }
  29. // ErrParse returns parse error.
  30. func ErrParse() *Error {
  31. return &Error{
  32. Code: ErrorCodeParse,
  33. Message: "Parse error",
  34. }
  35. }
  36. // ErrInvalidRequest returns invalid request error.
  37. func ErrInvalidRequest() *Error {
  38. return &Error{
  39. Code: ErrorCodeInvalidRequest,
  40. Message: "Invalid Request",
  41. }
  42. }
  43. // ErrMethodNotFound returns method not found error.
  44. func ErrMethodNotFound() *Error {
  45. return &Error{
  46. Code: ErrorCodeMethodNotFound,
  47. Message: "Method not found",
  48. }
  49. }
  50. // ErrInvalidParams returns invalid params error.
  51. func ErrInvalidParams() *Error {
  52. return &Error{
  53. Code: ErrorCodeInvalidParams,
  54. Message: "Invalid params",
  55. }
  56. }
  57. // ErrInternal returns internal error.
  58. func ErrInternal() *Error {
  59. return &Error{
  60. Code: ErrorCodeInternal,
  61. Message: "Internal error",
  62. }
  63. }