-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
45 lines (34 loc) · 1.59 KB
/
errors.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
package stretchr
import (
"errors"
"github.com/stretchr/sdk-go/api"
)
var (
ErrSingleObjectExpectedButGotArray = errors.New("stretchr: Array in response data when a single object was expected.")
ErrSingleObjectExpectedButGotNil = errors.New("stretchr: Nil in response data when a single object was expected.")
ErrSingleObjectExpectedButGotSomethingElse = errors.New("stretchr: Unexpected thing in response data when a single object was expected.")
ErrArrayObjectExpectedButGotNil = errors.New("stretchr: Nil is response data when a collection of objects (array) was expected.")
ErrArrayObjectExpectedButGotSingleObject = errors.New("stretchr: Single object in response data when a collection of objects (array) was expected.")
ErrArrayObjectExpectedButGotSomethingElse = errors.New("stretchr: Unexpected thing in response data when a collection of objects (array) was expected.")
)
// GetErrorsFromResponseObject gets the array of errors from the ResponseObject.
func GetErrorsFromResponseObject(response api.ResponseObject) []error {
errorStrings := response.Errors()
if len(errorStrings) > 0 {
errorArray := make([]error, len(errorStrings))
for errIndex, errString := range errorStrings {
errorArray[errIndex] = errors.New(errString)
}
return errorArray
}
return []error{}
}
// GetErrorFromResponseObject gets the first error from the ResponseObject, or returns nil
// if there were no errors.
func GetErrorFromResponseObject(response api.ResponseObject) error {
errs := GetErrorsFromResponseObject(response)
if len(errs) == 0 {
return nil
}
return errs[0]
}