Codebase list golang-github-bluebreezecf-opentsdb-goclient / 4bbfd33
Parse and return error info if putting returns non-200. bluebreezecf 8 years ago
1 changed file(s) with 19 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
6161 type PutError struct {
6262 Data DataPoint `json:"datapoint"`
6363 ErrorMsg string `json:"error"`
64 }
65
66 func (putErr *PutError) String() string {
67 return fmt.Sprintf("%s:%s", putErr.ErrorMsg, putErr.Data.String())
6468 }
6569
6670 // PutResponse acts as the implementation of Response
111115 if err = c.sendRequest(PostMethod, putEndpoint, reqBodyCnt, &putResp); err != nil {
112116 return nil, err
113117 }
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())
115133 }
116134
117135 func getPutBodyContents(datas []DataPoint) (string, error) {