divan/gorilla-xmlrpc

Error handling at client Side

Closed this issue · 2 comments

I am new to golang and xml-rpc .I am using gorilla-xmlrpc package for xmlserver and client. When Server is returning fault my client is not able to handle it. As your DecodeClientResponse function is calling ioutill.ReadAll function the response received is getting lost thus i am not able to do decode it twice.
I am attaching my server and client code.
Server Code:
type Yahoo struct {
First string
Second string
}
type YahooResp struct {
Yo int
Man string
}
type Newe struct{}

func (h *Newe) Yup(r *http.Request, recvRequest *Yahoo, respLogin *YahooResp) error {
fmt.Println(recvRequest.First)
fmt.Println(recvRequest.Second)
respLogin.Yo = 1
respLogin.Man = "response"
return nil
}

Client Code:
type Yahoo struct {
First int //changed from server so that server will return error
Second string
}
type YahooResp struct {
Yo int
Man string
}

func TestFun() {
var args Yahoo
var myfault xml.Fault
var reply YahooResp
var copyBody io.Reader
args.First = 10
args.Second = "Wewin"
buf, _ := xml.EncodeClientRequest("Newe.Yup", &args)
body := bytes.NewBuffer(buf)
fmt.Printf("data to send %s \n", string(buf))
resp, err := http.Post("http://localhost:7541/xmlrpc", "text/xml", body)
if err != nil {
return
}
defer resp.Body.Close()
err = xml.DecodeClientResponse(resp.Body, &reply)
if err != nil {
fmt.Printf("Error is %v\n", err.Error())
err = xml.DecodeClientResponse(resp.Body, &myfault)
fmt.Printf("Myfault code is %d\n", myfault.Code)
fmt.Printf("Myfault string is %s\n", myfault.String)
} else {
fmt.Println(reply.Yo)
}
return
}

Hi arjitkgupta, sorry for late reply. I've add Faults handling, and in your case you can cast err from DecodeClientResponse to type Fault, which has accessible fields Code and String.

Something like this (see fault_test.go for example):

err = xml.DecodeClientResponse(resp.Body, &reply)
fault, ok := err.(Fault)
if ok {
    fmt.Println("Fault code: %d, Fault string: %s", fault.Code, fault.String)
}

Note, that err is of interface type error, and not has to be of type Fault, so be sure to check it with comma-ok syntax.

Please check if it solves your problem.

Assuming issue is solved.