Skip to content

Commit f3b2cf4

Browse files
authored
chore: remove usage of panic (#3598)
* chore: remove usage of panic * Fix lint
1 parent 430d4ca commit f3b2cf4

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed

.github/workflows/lint.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ jobs:
4848
run: make tfproviderlint
4949
- name: Run tfproviderlintx
5050
run: make tfproviderlintx
51+
- name: Run gopaniccheck
52+
run: make gopaniccheck
5153

5254
tfproviderdocs:
5355
name: tfproviderdocs

GNUmakefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ tfproviderdocs:
6666
tfproviderlintx:
6767
go tool tfproviderlintx -S013=false -XR001=false -XS002=false ./...
6868

69+
gopaniccheck:
70+
go tool gopaniccheck ./...
71+
6972
format_examples:
7073
terraform fmt -recursive examples
7174

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ require (
182182
)
183183

184184
tool (
185+
github.com/bflad/gopaniccheck/cmd/gopaniccheck
185186
github.com/bflad/tfproviderdocs
186187
github.com/bflad/tfproviderlint/cmd/tfproviderlint
187188
github.com/bflad/tfproviderlint/cmd/tfproviderlintx

internal/acctest/acctest_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,9 @@ var objectBodyCassette = `{
343343
func newRequest(method, url string, body io.Reader) *http.Request {
344344
req, err := http.NewRequestWithContext(context.Background(), method, url, body)
345345
if err != nil {
346-
panic(err) // lintignore: R009
346+
// In test context, we can log the error and return nil since this is a test helper
347+
// The test will fail when trying to use the nil request
348+
return nil
347349
}
348350

349351
return req

internal/acctest/vcr.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,16 @@ func cassetteBodyMatcher(request *http.Request, cassette cassette.Request) bool
102102

103103
r, err := request.GetBody()
104104
if err != nil {
105-
panic(fmt.Errorf("cassette body matcher: failed to copy request body: %w", err)) // lintignore: R009
105+
logging.L.Errorf("cassette body matcher: failed to copy request body: %v", err)
106+
107+
return false
106108
}
107109

108110
requestBody, err := io.ReadAll(r)
109111
if err != nil {
110-
panic(fmt.Errorf("cassette body matcher: failed to read actualRequest body: %w", err)) // lintignore: R009
112+
logging.L.Errorf("cassette body matcher: failed to read actualRequest body: %v", err)
113+
114+
return false
111115
}
112116

113117
// Try to match raw bodies if they are not JSON (ex: cloud-init config)
@@ -127,7 +131,9 @@ func cassetteBodyMatcher(request *http.Request, cassette cassette.Request) bool
127131
if !json.Valid(requestBody) {
128132
requestValues, err := url.ParseQuery(string(requestBody))
129133
if err != nil {
130-
panic(fmt.Errorf("cassette body matcher: failed to parse body as url values: %w", err)) // lintignore: R009
134+
logging.L.Errorf("cassette body matcher: failed to parse body as url values: %v", err)
135+
136+
return false
131137
}
132138

133139
// Remove keys that should be ignored during comparison
@@ -140,7 +146,9 @@ func cassetteBodyMatcher(request *http.Request, cassette cassette.Request) bool
140146

141147
err = json.Unmarshal(requestBody, &requestJSON)
142148
if err != nil {
143-
panic(fmt.Errorf("cassette body matcher: failed to parse request body as json: %w", err)) // lintignore: R009
149+
logging.L.Errorf("cassette body matcher: failed to parse request body as json: %v", err)
150+
151+
return false
144152
}
145153

146154
err = json.Unmarshal([]byte(cassette.Body), &cassetteJSON)

0 commit comments

Comments
 (0)