2022-01-31 20:55:38 +03:00
|
|
|
//Package rpc provides abstract rpc server
|
|
|
|
//
|
|
|
|
//Copyright (C) 2022 Alexander Kiryukhin <i@neonxp.dev>
|
|
|
|
//
|
2024-04-28 00:18:38 +03:00
|
|
|
//This file is part of go.neonxp.ru/jsonrpc2 project.
|
2022-01-31 20:55:38 +03:00
|
|
|
//
|
|
|
|
//This program is free software: you can redistribute it and/or modify
|
|
|
|
//it under the terms of the GNU General Public License as published by
|
|
|
|
//the Free Software Foundation, either version 3 of the License, or
|
|
|
|
//(at your option) any later version.
|
|
|
|
//
|
|
|
|
//This program is distributed in the hope that it will be useful,
|
|
|
|
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
//GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
//You should have received a copy of the GNU General Public License
|
|
|
|
//along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2022-01-31 20:17:31 +03:00
|
|
|
package rpc
|
2022-01-31 02:31:43 +03:00
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
const (
|
|
|
|
ErrCodeParseError = -32700
|
|
|
|
ErrCodeInvalidRequest = -32600
|
|
|
|
ErrCodeMethodNotFound = -32601
|
|
|
|
ErrCodeInvalidParams = -32602
|
|
|
|
ErrCodeInternalError = -32603
|
|
|
|
ErrUser = -32000
|
|
|
|
)
|
|
|
|
|
|
|
|
var errorMap = map[int]string{
|
2022-05-28 16:53:20 +03:00
|
|
|
ErrCodeParseError: "Parse error", // Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.
|
|
|
|
ErrCodeInvalidRequest: "Invalid Request", // The JSON sent is not a valid Request object.
|
|
|
|
ErrCodeMethodNotFound: "Method not found", // The method does not exist / is not available.
|
|
|
|
ErrCodeInvalidParams: "Invalid params", // Invalid method parameter(s).
|
|
|
|
ErrCodeInternalError: "Internal error", // Internal JSON-RPC error.
|
|
|
|
ErrUser: "Other error",
|
2022-01-31 02:31:43 +03:00
|
|
|
}
|
|
|
|
|
2022-01-31 20:17:31 +03:00
|
|
|
//-32000 to -32099 RpcServer error Reserved for implementation-defined server-errors.
|
2022-01-31 02:31:43 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-05-22 03:27:50 +03:00
|
|
|
func ErrorFromCode(code int) Error {
|
2022-01-31 02:31:43 +03:00
|
|
|
if _, ok := errorMap[code]; ok {
|
|
|
|
return Error{
|
|
|
|
Code: code,
|
|
|
|
Message: errorMap[code],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Error{Code: code}
|
|
|
|
}
|
2022-05-22 03:27:50 +03:00
|
|
|
|
|
|
|
func NewError(message string, code int) Error {
|
|
|
|
if code == 0 {
|
|
|
|
code = ErrUser
|
|
|
|
}
|
|
|
|
return Error{
|
|
|
|
Code: code,
|
|
|
|
Message: message,
|
|
|
|
}
|
|
|
|
}
|