Probe revamped to provide for a new WrappedError struct to wrap probes as error interface

This convenience was necessary to be used for golang library functions like io.Copy and io.Pipe
where we shouldn't be writing proxies and alternatives returning *probe.Error

This change also brings more changes across code base for clear separation regarding where an error
interface should be passed encapsulating *probe.Error and where it should be used as is.
This commit is contained in:
Harshavardhana
2015-08-07 23:47:22 -07:00
parent 28d9565400
commit 45b59b8456
34 changed files with 392 additions and 363 deletions

View File

@@ -38,74 +38,70 @@ func GetMemStats(url string) ([]byte, *probe.Error) {
Method: "MemStats.Get",
Request: rpc.Args{Request: ""},
}
req, err := NewRequest(url, op, http.DefaultTransport)
if err != nil {
return nil, err.Trace()
req, perr := NewRequest(url, op, http.DefaultTransport)
if perr != nil {
return nil, perr.Trace()
}
resp, err := req.Do()
resp, perr := req.Do()
defer closeResp(resp)
if err != nil {
return nil, err.Trace()
if perr != nil {
return nil, perr.Trace()
}
var reply rpc.MemStatsReply
if err := jsonrpc.DecodeClientResponse(resp.Body, &reply); err != nil {
return nil, probe.New(err)
return nil, probe.NewError(err)
}
{
jsonRespBytes, err := json.MarshalIndent(reply, "", "\t")
if err != nil {
return nil, probe.New(err)
}
return jsonRespBytes, nil
jsonRespBytes, err := json.MarshalIndent(reply, "", "\t")
if err != nil {
return nil, probe.NewError(err)
}
return jsonRespBytes, nil
}
// GetSysInfo get system status of the server at given url
func GetSysInfo(url string) ([]byte, error) {
func GetSysInfo(url string) ([]byte, *probe.Error) {
op := RPCOps{
Method: "SysInfo.Get",
Request: rpc.Args{Request: ""},
}
req, err := NewRequest(url, op, http.DefaultTransport)
if err != nil {
return nil, err.Trace()
req, perr := NewRequest(url, op, http.DefaultTransport)
if perr != nil {
return nil, perr.Trace()
}
resp, err := req.Do()
resp, perr := req.Do()
defer closeResp(resp)
if err != nil {
return nil, err.Trace()
if perr != nil {
return nil, perr.Trace()
}
var reply rpc.SysInfoReply
if err := jsonrpc.DecodeClientResponse(resp.Body, &reply); err != nil {
return nil, probe.New(err)
return nil, probe.NewError(err)
}
{
jsonRespBytes, err := json.MarshalIndent(reply, "", "\t")
if err != nil {
return nil, probe.New(err)
}
return jsonRespBytes, nil
jsonRespBytes, err := json.MarshalIndent(reply, "", "\t")
if err != nil {
return nil, probe.NewError(err)
}
return jsonRespBytes, nil
}
// GetAuthKeys get access key id and secret access key
func GetAuthKeys(url string) ([]byte, error) {
func GetAuthKeys(url string) ([]byte, *probe.Error) {
op := RPCOps{
Method: "Auth.Get",
Request: rpc.Args{Request: ""},
}
req, err := NewRequest(url, op, http.DefaultTransport)
if err != nil {
return nil, err.Trace()
req, perr := NewRequest(url, op, http.DefaultTransport)
if perr != nil {
return nil, perr.Trace()
}
resp, err := req.Do()
resp, perr := req.Do()
defer closeResp(resp)
if err != nil {
return nil, err.Trace()
if perr != nil {
return nil, perr.Trace()
}
var reply rpc.AuthReply
if err := jsonrpc.DecodeClientResponse(resp.Body, &reply); err != nil {
return nil, probe.New(err)
return nil, probe.NewError(err)
}
authConfig := &auth.Config{}
authConfig.Version = "0.0.1"
@@ -118,13 +114,11 @@ func GetAuthKeys(url string) ([]byte, error) {
if err := auth.SaveConfig(authConfig); err != nil {
return nil, err.Trace()
}
{
jsonRespBytes, err := json.MarshalIndent(reply, "", "\t")
if err != nil {
return nil, probe.New(err)
}
return jsonRespBytes, nil
jsonRespBytes, err := json.MarshalIndent(reply, "", "\t")
if err != nil {
return nil, probe.NewError(err)
}
return jsonRespBytes, nil
}
// Add more functions here for other RPC messages

View File

@@ -40,11 +40,11 @@ type RPCRequest struct {
func NewRequest(url string, op RPCOps, transport http.RoundTripper) (*RPCRequest, *probe.Error) {
params, err := json.EncodeClientRequest(op.Method, op.Request)
if err != nil {
return nil, probe.New(err)
return nil, probe.NewError(err)
}
req, err := http.NewRequest("POST", url, bytes.NewReader(params))
if err != nil {
return nil, probe.New(err)
return nil, probe.NewError(err)
}
rpcReq := &RPCRequest{}
rpcReq.req = req
@@ -60,7 +60,10 @@ func NewRequest(url string, op RPCOps, transport http.RoundTripper) (*RPCRequest
func (r RPCRequest) Do() (*http.Response, *probe.Error) {
resp, err := r.transport.RoundTrip(r.req)
if err != nil {
return nil, probe.New(err)
if werr, ok := probe.ToWrappedError(err); ok {
return nil, werr.ToError().Trace()
}
return nil, probe.NewError(err)
}
return resp, nil
}