forked from simpleforce/simpleforce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errorHelpers.go
74 lines (63 loc) · 1.71 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package simpleforce
import (
"encoding/json"
"encoding/xml"
"fmt"
"github.com/pkg/errors"
)
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"`
}
type SalesforceError struct {
Message string
HttpCode int
ErrorCode string
ErrorMessage string
}
func (err SalesforceError) Error() string {
return err.Message
}
//Need to get information out of this package.
func ParseSalesforceError(statusCode int, responseBody []byte) (err error) {
jsonError := jsonError{}
err = json.Unmarshal(responseBody, &jsonError)
if err == nil {
return SalesforceError{
Message: fmt.Sprintf(
logPrefix+" Error. http code: %v Error Message: %v Error Code: %v",
statusCode, jsonError[0].Message, jsonError[0].ErrorCode,
),
HttpCode: statusCode,
ErrorCode: jsonError[0].ErrorCode,
ErrorMessage: jsonError[0].Message,
}
}
xmlError := xmlError{}
err = xml.Unmarshal(responseBody, &xmlError)
if err == nil {
return SalesforceError{
Message: fmt.Sprintf(
logPrefix+" Error. http code: %v Error Message: %v Error Code: %v",
statusCode, xmlError.Message, xmlError.ErrorCode,
),
HttpCode: statusCode,
ErrorCode: xmlError.ErrorCode,
ErrorMessage: xmlError.Message,
}
}
return SalesforceError{
Message: string(responseBody),
HttpCode: statusCode,
}
}