Skip to content

Commit 96c7dc8

Browse files
committed
Address CodeRabbit: InvalidCause unwrap and table-driven error tests
- InvalidError can wrap a cause via InvalidCause + Unwrap - REST handlers use InvalidCause for ParseForm/currency/amount failures - Currency validation uses a dedicated message, not amount-creation copy - Expand error package tests with table-driven parallel cases
1 parent 77844ed commit 96c7dc8

7 files changed

Lines changed: 209 additions & 110 deletions

File tree

controller/rest/intent/cancel/restintentcancel.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ const (
1818

1919
responseTye = "application/json"
2020

21-
errorParsingParam = "error during the payload parsing: '%v'"
21+
errorParsingParam = "error during the payload parsing"
2222
errorParamPayloadMissing = "missing payload mandatory parameters to cancel a payment intent"
23-
errorAmountCreation = "error during the intent amount creation: '%v'"
24-
errorIntentEncoding = "error during the intent encoding: '%v'"
23+
errorCurrencyParsing = "error during the currency parsing"
24+
errorIntentEncoding = "error during the intent encoding: %w"
2525
)
2626

2727
// @Summary Cancel an intent
@@ -62,7 +62,7 @@ func getParams(r *http.Request) (string, appcurrency.Currency, error) {
6262
ID := vars["id"]
6363

6464
if e := r.ParseForm(); e != nil {
65-
return "", nil, apperror.Invalid(fmt.Sprintf(errorParsingParam, e))
65+
return "", nil, apperror.InvalidCause(errorParsingParam, e)
6666
}
6767

6868
p := r.Form
@@ -72,7 +72,7 @@ func getParams(r *http.Request) (string, appcurrency.Currency, error) {
7272

7373
cur, e := appcurrency.New(p.Get("currency"))
7474
if e != nil {
75-
return "", nil, apperror.Invalid(fmt.Sprintf(errorAmountCreation, e))
75+
return "", nil, apperror.InvalidCause(errorCurrencyParsing, e)
7676
}
7777

7878
return ID, cur, nil

controller/rest/intent/capture/restintentcapture.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ const (
1818

1919
responseTye = "application/json"
2020

21-
errorParsingParam = "error during the payload parsing: '%v'"
21+
errorParsingParam = "error during the payload parsing"
2222
errorParamPayloadMissing = "missing payload mandatory parameters to capture a payment intent"
23-
errorAmountCreation = "error during the intent amount creation: '%v'"
24-
errorIntentEncoding = "error during the intent encoding: '%v'"
23+
errorCurrencyParsing = "error during the currency parsing"
24+
errorIntentEncoding = "error during the intent encoding: %w"
2525
)
2626

2727
// @Summary Capture an intent
@@ -62,7 +62,7 @@ func getParams(r *http.Request) (string, appcurrency.Currency, error) {
6262
ID := vars["id"]
6363

6464
if e := r.ParseForm(); e != nil {
65-
return "", nil, apperror.Invalid(fmt.Sprintf(errorParsingParam, e))
65+
return "", nil, apperror.InvalidCause(errorParsingParam, e)
6666
}
6767

6868
p := r.Form
@@ -72,7 +72,7 @@ func getParams(r *http.Request) (string, appcurrency.Currency, error) {
7272

7373
cur, e := appcurrency.New(p.Get("currency"))
7474
if e != nil {
75-
return "", nil, apperror.Invalid(fmt.Sprintf(errorAmountCreation, e))
75+
return "", nil, apperror.InvalidCause(errorCurrencyParsing, e)
7676
}
7777

7878
return ID, cur, nil

controller/rest/intent/confirm/restintentconfirm.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ const (
1818

1919
responseTye = "application/json"
2020

21-
errorParsingParam = "error during the payload parsing: '%v'"
21+
errorParsingParam = "error during the payload parsing"
2222
errorParamPayloadMissing = "missing payload mandatory parameters to confirm a payment intent"
23-
errorAmountCreation = "error during the intent amount creation: '%v'"
24-
errorIntentEncoding = "error during the intent encoding: '%v'"
23+
errorCurrencyParsing = "error during the currency parsing"
24+
errorIntentEncoding = "error during the intent encoding: %w"
2525
)
2626

2727
// @Summary Confirm an intent
@@ -62,7 +62,7 @@ func getParams(r *http.Request) (string, appcurrency.Currency, error) {
6262
ID := vars["id"]
6363

6464
if e := r.ParseForm(); e != nil {
65-
return "", nil, apperror.Invalid(fmt.Sprintf(errorParsingParam, e))
65+
return "", nil, apperror.InvalidCause(errorParsingParam, e)
6666
}
6767

6868
p := r.Form
@@ -72,7 +72,7 @@ func getParams(r *http.Request) (string, appcurrency.Currency, error) {
7272

7373
cur, e := appcurrency.New(p.Get("currency"))
7474
if e != nil {
75-
return "", nil, apperror.Invalid(fmt.Sprintf(errorAmountCreation, e))
75+
return "", nil, apperror.InvalidCause(errorCurrencyParsing, e)
7676
}
7777

7878
return ID, cur, nil

controller/rest/intent/create/restintentcreate.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ const (
1919

2020
responseTye = "application/json"
2121

22-
errorParsingParam = "error during the payload parsing: '%v'"
22+
errorParsingParam = "error during the payload parsing"
2323
errorParamMissing = "missing payload mandatory parameters to create a payment intent"
24-
errorParamAmountType = "error during the amount conversion: '%v'"
25-
errorAmountCreation = "error during the intent amount creation: '%v'"
26-
errorIntentEncoding = "error during the intent encoding: '%v'"
24+
errorParamAmountType = "error during the amount conversion"
25+
errorAmountCreation = "error during the intent amount creation"
26+
errorIntentEncoding = "error during the intent encoding: %w"
2727
)
2828

2929
// @Summary Create an intent
@@ -63,7 +63,7 @@ func Handler(w http.ResponseWriter, r *http.Request) {
6363
// Get and transform the payload params into domain structs
6464
func getParams(r *http.Request) (appamount.Amount, appsource.Source, appcustomer.Customer, error) {
6565
if e := r.ParseForm(); e != nil {
66-
return nil, nil, nil, apperror.Invalid(fmt.Sprintf(errorParsingParam, e))
66+
return nil, nil, nil, apperror.InvalidCause(errorParsingParam, e)
6767
}
6868

6969
p := r.Form
@@ -73,12 +73,12 @@ func getParams(r *http.Request) (appamount.Amount, appsource.Source, appcustomer
7373

7474
ai, e := strconv.Atoi(p.Get("amount"))
7575
if e != nil {
76-
return nil, nil, nil, apperror.Invalid(fmt.Sprintf(errorParamAmountType, e))
76+
return nil, nil, nil, apperror.InvalidCause(errorParamAmountType, e)
7777
}
7878

7979
amount, e := appamount.New(ai, p.Get("currency"))
8080
if e != nil {
81-
return nil, nil, nil, apperror.Invalid(fmt.Sprintf(errorAmountCreation, e))
81+
return nil, nil, nil, apperror.InvalidCause(errorAmountCreation, e)
8282
}
8383

8484
var cus appcustomer.Customer

controller/rest/intent/get/restintentget.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ const (
1919
responseTye = "application/json"
2020

2121
errorParamQueryMissing = "error during the query parsing: missing currency"
22-
errorAmountCreation = "error during the intent amount creation: '%v'"
23-
errorIntentEncoding = "error during the intent encoding: '%v'"
22+
errorCurrencyParsing = "error during the currency parsing"
23+
errorIntentEncoding = "error during the intent encoding: %w"
2424
)
2525

2626
// @Summary Get an intent
@@ -67,7 +67,7 @@ func getParams(r *http.Request) (string, appcurrency.Currency, error) {
6767

6868
cur, e := appcurrency.New(cursym)
6969
if e != nil {
70-
return "", nil, apperror.Invalid(fmt.Sprintf(errorAmountCreation, e))
70+
return "", nil, apperror.InvalidCause(errorCurrencyParsing, e)
7171
}
7272

7373
return ID, cur, nil

0 commit comments

Comments
 (0)