aboutsummaryrefslogtreecommitdiff
path: root/rpc/errors.go
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-01-31 20:17:31 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-01-31 20:25:19 +0300
commit4cf58de9bb7b109dddbc80adefe52cdf61328d0d (patch)
treedbc4c46eb64672e077305aebbe694acd1acf4828 /rpc/errors.go
parent35fee8155bb6835d372c364ef721077b0dcb9d38 (diff)
Small refactoring
Diffstat (limited to 'rpc/errors.go')
-rw-r--r--rpc/errors.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/rpc/errors.go b/rpc/errors.go
new file mode 100644
index 0000000..1af84ee
--- /dev/null
+++ b/rpc/errors.go
@@ -0,0 +1,42 @@
+package rpc
+
+import "fmt"
+
+const (
+ ErrCodeParseError = -32700
+ ErrCodeInvalidRequest = -32600
+ ErrCodeMethodNotFound = -32601
+ ErrCodeInvalidParams = -32602
+ ErrCodeInternalError = -32603
+ ErrUser = -32000
+)
+
+var errorMap = map[int]string{
+ -32700: "Parse error", // Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.
+ -32600: "Invalid Request", // The JSON sent is not a valid Request object.
+ -32601: "Method not found", // The method does not exist / is not available.
+ -32602: "Invalid params", // Invalid method parameter(s).
+ -32603: "Internal error", // Internal JSON-RPC error.
+ -32000: "Other error",
+}
+
+//-32000 to -32099 RpcServer error Reserved for implementation-defined server-errors.
+
+type Error struct {
+ Code int `json:"code"`
+ Message string `json:"message"`
+}
+
+func (e Error) Error() string {
+ return fmt.Sprintf("jsonrpc2 error: code: %d message: %s", e.Code, e.Message)
+}
+
+func NewError(code int) Error {
+ if _, ok := errorMap[code]; ok {
+ return Error{
+ Code: code,
+ Message: errorMap[code],
+ }
+ }
+ return Error{Code: code}
+}