forked from simpleforce/simpleforce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrorHelpers.go
52 lines (46 loc) · 1.44 KB
/
errorHelpers.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
52
package simpleforce
import (
"encoding/json"
"encoding/xml"
"fmt"
"github.com/pkg/errors"
"log"
)
var (
// ErrFailure is a generic error if none of the other errors are appropriate.
ErrFailure = errors.New("general failure")
// ErrAuthentication is returned when authentication failed.
ErrAuthentication = errors.New("authentication failure")
)
type jsonError []struct {
Message string `json:"message"`
ErrorCode string `json:"errorCode"`
}
type xmlError struct {
Message string `xml:"Body>Fault>faultstring"`
ErrorCode string `xml:"Body>Fault>faultcode"`
}
//Need to get information out of this package.
func ParseSalesforceError(statusCode int, responseBody []byte) (err error) {
jsonError := jsonError{}
xmlError := xmlError{}
err = json.Unmarshal(responseBody, &jsonError)
if err != nil {
//Unable to parse json. Try xml
err = xml.Unmarshal(responseBody, &xmlError)
if err != nil {
//Unable to parse json or XML
log.Println("ERROR UNMARSHALLING: ", err)
return ErrFailure
}
//successfully parsed XML:
message := fmt.Sprintf(logPrefix+" Error. http code: %v Error Message: %v Error Code: %v", statusCode, xmlError.Message, xmlError.ErrorCode)
err = errors.New(message)
return err
} else {
//Successfully parsed json error:
message := fmt.Sprintf(logPrefix+" Error. http code: %v Error Message: %v Error Code: %v", statusCode, jsonError[0].Message, jsonError[0].ErrorCode)
err = errors.New(message)
return err
}
}