Initial cloud-services repo - gateway service + pkg modules

This commit is contained in:
Chris Rai
2026-01-30 23:14:52 -05:00
commit fbb820d7b3
1037 changed files with 171318 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
package utils
import (
"errors"
"fmt"
"net/http"
"fiskerinc.com/modules/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)
}