forked from giantswarm/kemp-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml.go
44 lines (36 loc) · 1.04 KB
/
xml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package kempclient
import (
"encoding/xml"
"fmt"
"io"
"github.com/juju/errgo"
"github.com/rogpeppe/go-charset/charset"
_ "github.com/rogpeppe/go-charset/data"
)
type ErrorResponse struct {
Debug string `xml:",innerxml"`
Error string
}
func (c *Client) parseResponse(reader io.Reader, result interface{}) error {
decoder := xml.NewDecoder(reader)
decoder.CharsetReader = charset.NewReader
return decoder.Decode(result)
}
func (c *Client) parseError(code int, reader io.Reader) error {
errorResponse := ErrorResponse{}
err := c.parseResponse(reader, &errorResponse)
if err != nil {
return errgo.NoteMask(err, fmt.Sprintf("kemp unable to parse error response '%s'", errorResponse.Debug), errgo.Any)
}
if c.debug {
fmt.Println("DEBUG:", errorResponse.Debug)
}
return errgo.Newf("%d - %s", code, errorResponse.Error)
}
func (c *Client) parseSuccess(reader io.Reader, data interface{}) error {
err := c.parseResponse(reader, data)
if err != nil {
return errgo.NoteMask(err, "kemp unable to parse response", errgo.Any)
}
return nil
}