-
Notifications
You must be signed in to change notification settings - Fork 46
/
errors.go
102 lines (91 loc) · 3.26 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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (c) HashiCorp Inc. All rights reserved.
// Licensed under the MPL-2.0 License. See NOTICE.txt in the project root for license information.
package odata
import (
"encoding/json"
"fmt"
"regexp"
"strings"
)
const (
ErrorAddedObjectReferencesAlreadyExist = "One or more added object references already exist"
ErrorCannotDeleteOrUpdateEnabledEntitlement = "Permission (scope or role) cannot be deleted or updated unless disabled first"
ErrorConflictingObjectPresentInDirectory = "A conflicting object with one or more of the specified property values is present in the directory"
ErrorNotValidReferenceUpdate = "Not a valid reference update"
ErrorPropertyValuesAreInvalid = "One or more property values specified are invalid"
ErrorResourceDoesNotExist = "Resource '.+' does not exist or one of its queried reference-property objects are not present"
ErrorRemovedObjectReferencesDoNotExist = "One or more removed object references do not exist"
ErrorServicePrincipalAppInOtherTenant = "When using this permission, the backing application of the service principal being created must in the local tenant"
ErrorServicePrincipalInvalidAppId = "The appId '.+' of the service principal does not reference a valid application object"
ErrorUnknownUnsupportedQuery = "UnknownError: Unsupported Query"
)
// Error is used to unmarshal an API error message.
type Error struct {
Code *string `json:"code"`
Date *string `json:"date"`
Message *string `json:"-"`
RawMessage *json.RawMessage `json:"message"` // sometimes a string, sometimes an object :/
ClientRequestId *string `json:"client-request-id"`
RequestId *string `json:"request-id"`
InnerError *Error `json:"innerError"` // nested errors
Details *[]struct {
Code *string `json:"code"`
Target *string `json:"target"`
} `json:"details"`
Values *[]struct {
Item string `json:"item"`
Value string `json:"value"`
} `json:"values"`
}
func (e *Error) UnmarshalJSON(data []byte) error {
// Perform unmarshalling using a local type
type error Error
var e2 error
if err := json.Unmarshal(data, &e2); err != nil {
return err
}
*e = Error(e2)
// Unmarshal the message, which can be a plain string or an object wrapping a message
if raw := e.RawMessage; raw != nil && len(*raw) > 0 {
switch string((*raw)[0]) {
case "\"":
var s string
if err := json.Unmarshal(*raw, &s); err != nil {
return err
}
e.Message = &s
case "{":
var m map[string]interface{}
if err := json.Unmarshal(*raw, &m); err != nil {
return err
}
if v, ok := m["value"]; ok {
if s, ok := v.(string); ok {
e.Message = &s
}
}
default:
return fmt.Errorf("unrecognised error message: %#v", string(*raw))
}
}
return nil
}
func (e Error) String() string {
sl := make([]string, 0)
if e.Code != nil {
sl = append(sl, *e.Code)
}
if e.Message != nil {
sl = append(sl, *e.Message)
}
if e.InnerError != nil {
if is := e.InnerError.String(); is != "" {
sl = append(sl, is)
}
}
return strings.Join(sl, ": ")
}
func (e Error) Match(errorText string) bool {
re := regexp.MustCompile(errorText)
return re.MatchString(e.String())
}