From 440f3f4604f3489113f6705b8da67d839e52360e Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Mon, 31 Jan 2022 02:31:43 +0300 Subject: initial --- errors.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 errors.go (limited to 'errors.go') diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..cd87fbb --- /dev/null +++ b/errors.go @@ -0,0 +1,42 @@ +package jsonrpc2 + +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 Server 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} +} -- cgit v1.2.3