gorilla/rpc

Custom error code passing in rpcv2/json2

dolftax opened this issue · 1 comments

From the spec, -32000 to -32099 are reserved for implementation-defined server-errors. On a service method, how to pass the error code and error message?

// NodeGetArgs defines the input parameters for node.Get
type NodeGetArgs struct {
	NodeID int64 `json:"node_id"`
}

// NodeGetReply defines the result data type for node.Get
type NodeGetReply struct {
	Node  models.Node `json:"node"`
}

// Get fetches the contents of a single node given a Node ID
func (n *NodeService) Get(r *http.Request, args *NodeGetArgs, reply *NodeGetReply) json2.Error {
	_node, err := models.GetNode(&args.NodeID)
	if err != nil {
		errorReply := json2.Error{
			Code:    -32001,
			Message: "Error fetching node",
			Data:    nil,
		}
		return errorReply
	}

	// Generate response
	reply.Node = _node
	return ?? // What to return here? Since `nil` isn't accepted
}
  • If the method returns error instead, how to pass a custom error code and a message?

You should be able to return a json2.Error even if the return type is error