Merge pull request #10 from tekkamanendless/return-meta-on-error
Return Meta on error
This commit is contained in:
		
							
								
								
									
										33
									
								
								client.go
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								client.go
									
									
									
									
									
								
							| @@ -3,7 +3,6 @@ package groupme | |||||||
| import ( | import ( | ||||||
| 	"bytes" | 	"bytes" | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"fmt" |  | ||||||
| 	"io/ioutil" | 	"io/ioutil" | ||||||
| 	"net/http" | 	"net/http" | ||||||
| ) | ) | ||||||
| @@ -70,28 +69,38 @@ func (c Client) do(req *http.Request, i interface{}) error { | |||||||
| 	} | 	} | ||||||
| 	defer getResp.Body.Close() | 	defer getResp.Body.Close() | ||||||
|  |  | ||||||
| 	bytes, err := ioutil.ReadAll(getResp.Body) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	// Check Status Code is 1XX or 2XX | 	// Check Status Code is 1XX or 2XX | ||||||
| 	if getResp.StatusCode/100 > 2 { | 	if getResp.StatusCode/100 > 2 { | ||||||
| 		return fmt.Errorf("%s: %s", getResp.Status, string(bytes)) | 		bytes, err := ioutil.ReadAll(getResp.Body) | ||||||
|  | 		if err != nil { | ||||||
|  | 			// We couldn't read the output.  Oh well; generate the appropriate error type anyway. | ||||||
|  | 			return &Meta{ | ||||||
|  | 				Code: HTTPStatusCode(getResp.StatusCode), | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		resp := newJSONResponse(nil) | ||||||
|  | 		if err := json.Unmarshal(bytes, &resp); err != nil { | ||||||
|  | 			// We couldn't parse the output.  Oh well; generate the appropriate error type anyway. | ||||||
|  | 			return &Meta{ | ||||||
|  | 				Code: HTTPStatusCode(getResp.StatusCode), | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		return &resp.Meta | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if i == nil { | 	if i == nil { | ||||||
| 		return nil | 		return nil | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	resp := newJSONResponse(i) | 	bytes, err := ioutil.ReadAll(getResp.Body) | ||||||
| 	if err := json.Unmarshal(bytes, &resp); err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Check Status Code is 1XX or 2XX | 	resp := newJSONResponse(i) | ||||||
| 	if resp.Meta.Code/100 > 2 { | 	if err := json.Unmarshal(bytes, &resp); err != nil { | ||||||
| 		return &resp.Meta | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return nil | 	return nil | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user