4
4
package tfresource
5
5
6
6
import (
7
+ "encoding/json"
7
8
"fmt"
8
9
"log"
9
10
"reflect"
10
11
"regexp"
12
+ "strconv"
11
13
"strings"
12
14
"time"
13
15
16
+ "github.com/oracle/terraform-provider-oci/internal/utils"
17
+
14
18
oci_common "github.com/oracle/oci-go-sdk/v65/common"
15
19
16
20
"github.com/oracle/terraform-provider-oci/internal/globalvar"
@@ -23,29 +27,31 @@ var serviceErrorCheck = func(err error) (failure oci_common.ServiceErrorLocaliza
23
27
}
24
28
25
29
const (
30
+ JsonErrorEnable = "PROVIDER_JSON_ERROR"
26
31
ServiceError errorTypeEnum = "ServiceError"
27
32
TimeoutError errorTypeEnum = "TimeoutError"
28
33
UnexpectedStateError errorTypeEnum = "UnexpectedStateError"
29
34
WorkRequestError errorTypeEnum = "WorkRequestError"
30
35
)
31
36
32
37
type customError struct {
33
- TypeOfError errorTypeEnum
34
- ErrorCode int
35
- ErrorCodeName string
36
- Service string
37
- Message string
38
- OriginalMessage string
39
- OriginalMessageTemplate string
40
- MessageArgument map [string ]string
41
- OpcRequestID string
42
- ResourceOCID string
43
- OperationName string
44
- RequestTarget string
45
- Suggestion string
46
- VersionError string
47
- ResourceDocs string
48
- SdkApiDocs string
38
+ TypeOfError errorTypeEnum `json:"type_of_error"`
39
+ ErrorCode int `json:"error_code"`
40
+ ErrorCodeName string `json:"error_code_name"`
41
+ Service string `json:"service"`
42
+ Message string `json:"message"`
43
+ OriginalMessage string `json:"original_message"`
44
+ OriginalMessageTemplate string `json:"original_message_template"`
45
+ MessageArgument map [string ]string `json:"message_argument"`
46
+ OpcRequestID string `json:"opc_request_id"`
47
+ ResourceOCID string `json:"resource_ocid"`
48
+ OperationName string `json:"operation_name"`
49
+ RequestTarget string `json:"request_target"`
50
+ Suggestion string `json:"suggestion"`
51
+ VersionError string `json:"version_error"`
52
+ ResourceDocs string `json:"resource_docs"`
53
+ SdkApiDocs string `json:"sdk_api_docs"`
54
+ JsonError string `json:"-"`
49
55
}
50
56
51
57
// Create new error format for Terraform output
@@ -102,10 +108,20 @@ func newCustomError(sync interface{}, err error) error {
102
108
103
109
tfError .VersionError = GetVersionAndDateError ()
104
110
tfError .Suggestion = getSuggestionFromError (tfError )
111
+ tfError .JsonError = getJsonError (tfError )
112
+
105
113
return tfError .Error ()
106
114
}
107
-
115
+ func getJsonError (tfError customError ) string {
116
+ errByte , err := json .Marshal (tfError )
117
+ if err != nil {
118
+ log .Printf ("[ERROR] Fail to marshal error: %v" , err )
119
+ return ""
120
+ }
121
+ return string (errByte )
122
+ }
108
123
func (tfE customError ) Error () error {
124
+ var finalError error
109
125
switch tfE .TypeOfError {
110
126
case ServiceError :
111
127
var serviceError string
@@ -129,33 +145,41 @@ func (tfE customError) Error() error {
129
145
// For compute out of host capacity error support
130
146
serviceError = shortErrorDescription + furtherInfo + detailedDescription
131
147
}
132
- return fmt .Errorf (serviceError )
148
+ finalError = fmt .Errorf (serviceError )
133
149
case TimeoutError :
134
- return fmt .Errorf ("%s \n " +
150
+ finalError = fmt .Errorf ("%s \n " +
135
151
"%s \n " +
136
152
"Service: %s \n " +
137
153
"Error Message: %s \n " +
138
154
"Suggestion: %s\n " ,
139
155
tfE .ErrorCodeName , tfE .VersionError , tfE .Service , tfE .Message , tfE .Suggestion )
140
156
case UnexpectedStateError :
141
- return fmt .Errorf ("%s \n " +
157
+ finalError = fmt .Errorf ("%s \n " +
142
158
"%s \n " +
143
159
"Service: %s \n " +
144
160
"Error Message: %s \n " +
145
161
"Resource OCID: %s \n " +
146
162
"Suggestion: %s\n " ,
147
163
tfE .ErrorCodeName , tfE .VersionError , tfE .Service , tfE .Message , tfE .ResourceOCID , tfE .Suggestion )
148
164
case WorkRequestError :
149
- return fmt .Errorf ("%s \n " +
165
+ finalError = fmt .Errorf ("%s \n " +
150
166
"%s \n " +
151
167
"Service: %s \n " +
152
168
"Error Message: %s \n " +
153
169
"Resource OCID: %s \n " +
154
170
"Suggestion: %s\n " ,
155
171
tfE .ErrorCodeName , tfE .VersionError , tfE .Service , tfE .Message , tfE .ResourceOCID , tfE .Suggestion )
156
172
default :
157
- return fmt .Errorf (tfE .Message )
173
+ finalError = fmt .Errorf (tfE .Message )
174
+ }
175
+ if isJsonErrorEnable , _ := strconv .ParseBool (utils .GetEnvSettingWithDefault (JsonErrorEnable , "false" )); isJsonErrorEnable {
176
+ finalError = fmt .Errorf ("%s \n " +
177
+ "JSON Error: %s \n " ,
178
+ finalError , tfE .JsonError )
158
179
}
180
+
181
+ return finalError
182
+
159
183
}
160
184
161
185
func handleMissingResourceError (sync ResourceVoider , err * error ) {
0 commit comments