Files
komari/utils/rpc/response.go

31 lines
1.0 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package rpc
// JsonRpcResponse JSON-RPC 2.0 响应
// 成功时包含 result失败时包含 error二者互斥。
// 在 Notification 情况下服务器不会发送任何响应。
type JsonRpcResponse struct {
Version string `json:"jsonrpc"`
ID any `json:"id,omitempty"`
Result any `json:"result,omitempty"`
Error *JsonRpcError `json:"error,omitempty"`
}
// SuccessResponse 构造成功响应
func SuccessResponse(id any, result any) *JsonRpcResponse {
return &JsonRpcResponse{Version: RPC_VERSION, ID: id, Result: result}
}
// ErrorResponse 构造失败响应
func ErrorResponse(id any, code int, msg string, data any) *JsonRpcResponse {
return &JsonRpcResponse{Version: RPC_VERSION, ID: id, Error: &JsonRpcError{Code: code, Message: msg, Data: data}}
}
// InternalErrorResponse 统一内部错误
func InternalErrorResponse(id any, err error) *JsonRpcResponse {
msg := "internal error"
if err != nil {
msg = err.Error()
}
return ErrorResponse(id, InternalError, msg, nil)
}