56 lines
1.8 KiB
Go
56 lines
1.8 KiB
Go
package utils
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/fiskerinc/cloud-services/pkg/common"
|
|
)
|
|
|
|
const ErrCodeJSONRPCParse = -32700
|
|
const ErrCodeJSONRPCInvalidRequest = -32600
|
|
const ErrCodeJSONRPCMethodNotFound = -32601
|
|
const ErrCodeJSONRPCInvalidParams = -32602
|
|
const ErrCodeJSONRPCInternalError = -32603
|
|
const ErrCodeJSONRPCServerError = -32000
|
|
|
|
var ErrJSONRPCParse = errors.New("Parse error")
|
|
var ErrJSONRPCInvalidRequest = errors.New("Invalid Request")
|
|
var ErrJSONRPCMethodNotFound = errors.New("Method not found")
|
|
var ErrJSONRPCInvalidParams = errors.New("Invalid params")
|
|
var ErrJSONRPCInternalError = errors.New("Internal error")
|
|
var ErrJSONRPCServerError = errors.New("Server error")
|
|
|
|
var ErrCodeJSONRPC map[error]int = map[error]int{
|
|
ErrJSONRPCParse: ErrCodeJSONRPCParse,
|
|
ErrJSONRPCInvalidRequest: ErrCodeJSONRPCInvalidRequest,
|
|
ErrJSONRPCMethodNotFound: ErrCodeJSONRPCMethodNotFound,
|
|
ErrJSONRPCInvalidParams: ErrCodeJSONRPCInvalidParams,
|
|
ErrJSONRPCInternalError: ErrCodeJSONRPCInternalError,
|
|
ErrJSONRPCServerError: ErrCodeJSONRPCServerError,
|
|
}
|
|
|
|
var StatsCodeJSONRPC map[error]int = map[error]int{
|
|
ErrJSONRPCParse: http.StatusBadRequest,
|
|
ErrJSONRPCInvalidRequest: http.StatusBadRequest,
|
|
ErrJSONRPCMethodNotFound: http.StatusNotFound,
|
|
ErrJSONRPCInvalidParams: http.StatusBadRequest,
|
|
ErrJSONRPCInternalError: http.StatusInternalServerError,
|
|
ErrJSONRPCServerError: http.StatusServiceUnavailable,
|
|
}
|
|
|
|
func RespJSONRPCError(w http.ResponseWriter, err error, msg string) {
|
|
code, ok := ErrCodeJSONRPC[err]
|
|
if !ok {
|
|
code = ErrCodeJSONRPCInternalError
|
|
}
|
|
resp := common.NewJSONRPCErrorResponse(code, fmt.Sprintf("%v. %s", err.Error(), msg))
|
|
status, ok := StatsCodeJSONRPC[err]
|
|
if !ok {
|
|
status = http.StatusInternalServerError
|
|
}
|
|
|
|
RespJSON(w, status, resp)
|
|
}
|