-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresponse.go
51 lines (43 loc) · 1.02 KB
/
response.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
45
46
47
48
49
50
51
package gosf
import (
"encoding/json"
"fmt"
"net/http"
)
type (
errResponse struct {
status string
statusCode int
errors []*sfErr
}
sfErr struct {
Message string `json:"message"`
ErrorCode string `json:"errorCode"`
}
)
func (e *sfErr) Error() string {
return e.ErrorCode + ":" + e.Message
}
func (e *errResponse) Error() string {
errStr := fmt.Sprintf("error response(%s) with %d errors:\n", e.status, len(e.errors))
for i, err := range e.errors {
errStr += fmt.Sprintln(i, ":", err)
}
return errStr
}
// parseErrResponse parses the error response from salesforce(response.StatusCode/100!=2)
func parseErrResponse(resp *http.Response) (err error) {
if resp.StatusCode/100 == 2 {
err = fmt.Errorf("reponse with success code %d can not be treated as an error response", resp.StatusCode)
return
}
errResp := &errResponse{
status: resp.Status,
statusCode: resp.StatusCode,
}
if err = json.NewDecoder(resp.Body).Decode(&errResp.errors); err != nil {
return
}
err = errResp
return
}