forked from g8rswimmer/go-sfdc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
63 lines (58 loc) · 1.5 KB
/
error.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
package sfdc
import (
"encoding/json"
"errors"
)
// Error is the error structure defined by the Salesforce API.
type Error struct {
ErrorCode string `json:"errorCode"`
Message string `json:"message"`
Fields []string `json:"fields"`
}
// UnmarshalJSON will unmarshal a JSON byte array.
func (e *Error) UnmarshalJSON(data []byte) error {
if e == nil {
return errors.New("record: can't unmarshal to a nil struct")
}
var jsonMap map[string]interface{}
err := json.Unmarshal(data, &jsonMap)
if err != nil {
return err
}
if code, ok := jsonMap["statusCode"]; ok {
if codeStr, ok := code.(string); ok {
e.ErrorCode = codeStr
} else {
return errors.New("json error: statusCode is not a string")
}
}
if code, ok := jsonMap["errorCode"]; ok {
if codeStr, ok := code.(string); ok {
e.ErrorCode = codeStr
} else {
return errors.New("json error: errorCode is not a string")
}
}
if message, ok := jsonMap["message"]; ok {
if messageStr, ok := message.(string); ok {
e.Message = messageStr
} else {
return errors.New("json error: message is not a string")
}
}
if fields, ok := jsonMap["fields"]; ok {
if array, has := fields.([]interface{}); has {
e.Fields = make([]string, len(array))
for idx, element := range array {
if field, ok := element.(string); ok {
e.Fields[idx] = field
} else {
return errors.New("json error: field element is not a string")
}
}
} else {
return errors.New("json error: fields is not an array")
}
}
return nil
}