Parse and return error info if putting returns non-200.
bluebreezecf
8 years ago
61 | 61 | type PutError struct { |
62 | 62 | Data DataPoint `json:"datapoint"` |
63 | 63 | ErrorMsg string `json:"error"` |
64 | } | |
65 | ||
66 | func (putErr *PutError) String() string { | |
67 | return fmt.Sprintf("%s:%s", putErr.ErrorMsg, putErr.Data.String()) | |
64 | 68 | } |
65 | 69 | |
66 | 70 | // PutResponse acts as the implementation of Response |
111 | 115 | if err = c.sendRequest(PostMethod, putEndpoint, reqBodyCnt, &putResp); err != nil { |
112 | 116 | return nil, err |
113 | 117 | } |
114 | return &putResp, nil | |
118 | if putResp.StatusCode == 200 { | |
119 | return &putResp, nil | |
120 | } | |
121 | return nil, parsePutErrorMsg(&putResp) | |
122 | } | |
123 | ||
124 | func parsePutErrorMsg(resp *PutResponse) error { | |
125 | buf := bytes.Buffer{} | |
126 | buf.WriteString(fmt.Sprintf("Failed to put %d datapoint(s) into opentsdb, statuscode %d:\n", resp.Failed, resp.StatusCode)) | |
127 | if len(resp.Errors) > 0 { | |
128 | for _, putError := range resp.Errors { | |
129 | buf.WriteString(fmt.Sprintf("\t%s\n", putError.String())) | |
130 | } | |
131 | } | |
132 | return errors.New(buf.String()) | |
115 | 133 | } |
116 | 134 | |
117 | 135 | func getPutBodyContents(datas []DataPoint) (string, error) { |