Skip to content

Commit 112380a

Browse files
committed
chore: add context to error messages in params validation
1 parent 31726a6 commit 112380a

File tree

6 files changed

+85
-81
lines changed

6 files changed

+85
-81
lines changed

x/lease/types/v1/params.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package v1
22

33
import (
44
"errors"
5+
"fmt"
56

67
sdkmath "cosmossdk.io/math"
78
)
@@ -16,15 +17,15 @@ var (
1617
// Validate checks whether the Params fields are valid according to defined rules.
1718
func (m *Params) Validate() error {
1819
if err := validateMaxHours(m.MaxHours); err != nil {
19-
return err
20+
return fmt.Errorf("invalid max_hours: %w", err)
2021
}
2122

2223
if err := validateMinHours(m.MinHours); err != nil {
23-
return err
24+
return fmt.Errorf("invalid min_hours: %w", err)
2425
}
2526

2627
if err := validateStakingShare(m.StakingShare); err != nil {
27-
return err
28+
return fmt.Errorf("invalid staking_share: %w", err)
2829
}
2930

3031
if m.MinHours > m.MaxHours {
@@ -54,25 +55,25 @@ func DefaultParams() Params {
5455

5556
// validateMaxHours checks that maxHours is a positive integer.
5657
func validateMaxHours(v int64) error {
57-
if v < 0 {
58-
return errors.New("max_hours cannot be negative")
58+
if v == 0 {
59+
return errors.New("value cannot be zero")
5960
}
6061

61-
if v == 0 {
62-
return errors.New("max_hours cannot be zero")
62+
if v < 0 {
63+
return errors.New("value cannot be negative")
6364
}
6465

6566
return nil
6667
}
6768

6869
// validateMinHours checks that minHours is a positive integer.
6970
func validateMinHours(v int64) error {
70-
if v < 0 {
71-
return errors.New("min_hours cannot be negative")
71+
if v == 0 {
72+
return errors.New("value cannot be zero")
7273
}
7374

74-
if v == 0 {
75-
return errors.New("min_hours cannot be zero")
75+
if v < 0 {
76+
return errors.New("value cannot be negative")
7677
}
7778

7879
return nil
@@ -84,15 +85,15 @@ func validateMinHours(v int64) error {
8485
// - Not greater than 1 (100%).
8586
func validateStakingShare(v sdkmath.LegacyDec) error {
8687
if v.IsNil() {
87-
return errors.New("staking_share cannot be nil")
88+
return errors.New("value cannot be nil")
8889
}
8990

9091
if v.IsNegative() {
91-
return errors.New("staking_share cannot be negative")
92+
return errors.New("value cannot be negative")
9293
}
9394

9495
if v.GT(sdkmath.LegacyOneDec()) {
95-
return errors.New("staking_share cannot be greater than 1")
96+
return errors.New("value cannot be greater than 1")
9697
}
9798

9899
return nil

x/node/types/v3/params.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@ func (m *Params) GetMinHourlyPrices() v1base.Prices {
5050
// Validate validates all parameters to ensure they conform to expected rules.
5151
func (m *Params) Validate() error {
5252
if err := validateActiveDuration(m.ActiveDuration); err != nil {
53-
return err
53+
return fmt.Errorf("invalid active_duration: %w", err)
5454
}
5555

5656
if err := validateDeposit(m.Deposit); err != nil {
57-
return err
57+
return fmt.Errorf("invalid deposit: %w", err)
5858
}
5959

6060
if err := validateMinGigabytePrices(m.MinGigabytePrices); err != nil {
61-
return err
61+
return fmt.Errorf("invalid min_gigabyte_prices: %w", err)
6262
}
6363

6464
if err := validateMinHourlyPrices(m.MinHourlyPrices); err != nil {
65-
return err
65+
return fmt.Errorf("invalid min_hourly_prices: %w", err)
6666
}
6767

6868
return nil
@@ -92,12 +92,12 @@ func DefaultParams() Params {
9292

9393
// validateActiveDuration checks that the active duration is greater than zero.
9494
func validateActiveDuration(v time.Duration) error {
95-
if v < 0 {
96-
return errors.New("active_duration cannot be negative")
95+
if v == 0 {
96+
return errors.New("value cannot be zero")
9797
}
9898

99-
if v == 0 {
100-
return errors.New("active_duration cannot be zero")
99+
if v < 0 {
100+
return errors.New("value cannot be negative")
101101
}
102102

103103
return nil
@@ -106,15 +106,15 @@ func validateActiveDuration(v time.Duration) error {
106106
// validateDeposit checks that the deposit is not nil, not negative, and valid.
107107
func validateDeposit(v sdk.Coin) error {
108108
if v.IsNil() {
109-
return errors.New("deposit cannot be nil")
109+
return errors.New("value cannot be nil")
110110
}
111111

112112
if v.IsNegative() {
113-
return errors.New("deposit cannot be negative")
113+
return errors.New("value cannot be negative")
114114
}
115115

116116
if !v.IsValid() {
117-
return fmt.Errorf("invalid deposit %s", v)
117+
return errors.New("invalid value")
118118
}
119119

120120
return nil
@@ -127,7 +127,7 @@ func validateMinGigabytePrices(v []v1base.Price) error {
127127
}
128128

129129
if err := v1base.Prices(v).Validate(); err != nil {
130-
return fmt.Errorf("invalid min_gigabyte_prices: %w", err)
130+
return fmt.Errorf("invalid value: %w", err)
131131
}
132132

133133
return nil
@@ -140,7 +140,7 @@ func validateMinHourlyPrices(v []v1base.Price) error {
140140
}
141141

142142
if err := v1base.Prices(v).Validate(); err != nil {
143-
return fmt.Errorf("invalid min_hourly_prices: %w", err)
143+
return fmt.Errorf("invalid value: %w", err)
144144
}
145145

146146
return nil

x/oracle/types/v1/params.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package v1
22

33
import (
44
"errors"
5+
"fmt"
56
"time"
67
)
78

@@ -15,15 +16,15 @@ var (
1516
// Validate checks whether the Params fields are valid according to defined rules.
1617
func (m *Params) Validate() error {
1718
if err := validateBlockInterval(m.BlockInterval); err != nil {
18-
return err
19+
return fmt.Errorf("invalid block_interval: %w", err)
1920
}
2021

2122
if err := validateChannelID(m.ChannelID); err != nil {
22-
return err
23+
return fmt.Errorf("invalid channel_id: %w", err)
2324
}
2425

2526
if err := validateTimeout(m.Timeout); err != nil {
26-
return err
27+
return fmt.Errorf("invalid timeout: %w", err)
2728
}
2829

2930
return nil
@@ -49,12 +50,12 @@ func DefaultParams() Params {
4950

5051
// validateBlockInterval checks that the block interval is a positive value.
5152
func validateBlockInterval(v int64) error {
52-
if v < 0 {
53-
return errors.New("block_interval cannot be negative")
53+
if v == 0 {
54+
return errors.New("value cannot not be zero")
5455
}
5556

56-
if v == 0 {
57-
return errors.New("block_interval cannot not be zero")
57+
if v < 0 {
58+
return errors.New("value cannot be negative")
5859
}
5960

6061
return nil
@@ -67,12 +68,12 @@ func validateChannelID(_ string) error {
6768

6869
// validateTimeout ensures the timeout duration is positive.
6970
func validateTimeout(v time.Duration) error {
70-
if v < 0 {
71-
return errors.New("timeout cannot be negative")
71+
if v == 0 {
72+
return errors.New("value cannot be zero")
7273
}
7374

74-
if v == 0 {
75-
return errors.New("timeout cannot be zero")
75+
if v < 0 {
76+
return errors.New("value cannot be negative")
7677
}
7778

7879
return nil

x/provider/types/v3/params.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var (
1515
// Validate checks that all parameters in Params are valid.
1616
func (m *Params) Validate() error {
1717
if err := validateDeposit(m.Deposit); err != nil {
18-
return err
18+
return fmt.Errorf("invalid deposit: %w", err)
1919
}
2020

2121
return nil
@@ -36,15 +36,15 @@ func DefaultParams() Params {
3636
// validateDeposit checks that the deposit is a valid, non-negative, non-nil coin.
3737
func validateDeposit(v sdk.Coin) error {
3838
if v.IsNil() {
39-
return errors.New("deposit cannot be nil")
39+
return errors.New("value cannot be nil")
4040
}
4141

4242
if v.IsNegative() {
43-
return errors.New("deposit cannot be negative")
43+
return errors.New("value cannot be negative")
4444
}
4545

4646
if err := v.Validate(); err != nil {
47-
return fmt.Errorf("invalid deposit: %w", err)
47+
return fmt.Errorf("invalid value: %w", err)
4848
}
4949

5050
return nil

x/session/types/v3/params.go

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package v3
22

33
import (
44
"errors"
5+
"fmt"
56
"time"
67

78
"cosmossdk.io/math"
@@ -21,31 +22,31 @@ var (
2122
// Validate checks whether the Params fields are valid according to defined rules.
2223
func (m *Params) Validate() error {
2324
if err := validateMaxGigabytes(m.MaxGigabytes); err != nil {
24-
return err
25+
return fmt.Errorf("invalid max_gigabytes: %w", err)
2526
}
2627

2728
if err := validateMinGigabytes(m.MinGigabytes); err != nil {
28-
return err
29+
return fmt.Errorf("invalid min_gigabytes: %w", err)
2930
}
3031

3132
if err := validateMaxHours(m.MaxHours); err != nil {
32-
return err
33+
return fmt.Errorf("invalid max_hours: %w", err)
3334
}
3435

3536
if err := validateMinHours(m.MinHours); err != nil {
36-
return err
37+
return fmt.Errorf("invalid min_hours: %w", err)
3738
}
3839

3940
if err := validateProofVerificationEnabled(m.ProofVerificationEnabled); err != nil {
40-
return err
41+
return fmt.Errorf("invalid proof_verification_enabled: %w", err)
4142
}
4243

4344
if err := validateStakingShare(m.StakingShare); err != nil {
44-
return err
45+
return fmt.Errorf("invalid staking_share: %w", err)
4546
}
4647

4748
if err := validateStatusChangeDelay(m.StatusChangeDelay); err != nil {
48-
return err
49+
return fmt.Errorf("invalid status_change_delay: %w", err)
4950
}
5051

5152
return nil
@@ -82,51 +83,51 @@ func DefaultParams() Params {
8283

8384
// validateMaxGigabytes ensures maxGigabytes is a positive non-zero integer.
8485
func validateMaxGigabytes(v int64) error {
85-
if v < 0 {
86-
return errors.New("max_gigabytes cannot be negative")
86+
if v == 0 {
87+
return errors.New("value cannot be zero")
8788
}
8889

89-
if v == 0 {
90-
return errors.New("max_gigabytes cannot be zero")
90+
if v < 0 {
91+
return errors.New("value cannot be negative")
9192
}
9293

9394
return nil
9495
}
9596

9697
// validateMinGigabytes ensures minGigabytes is a positive non-zero integer.
9798
func validateMinGigabytes(v int64) error {
98-
if v < 0 {
99-
return errors.New("min_gigabytes cannot be negative")
99+
if v == 0 {
100+
return errors.New("value cannot be zero")
100101
}
101102

102-
if v == 0 {
103-
return errors.New("min_gigabytes cannot be zero")
103+
if v < 0 {
104+
return errors.New("value cannot be negative")
104105
}
105106

106107
return nil
107108
}
108109

109110
// validateMaxHours ensures maxHours is a positive non-zero integer.
110111
func validateMaxHours(v int64) error {
111-
if v < 0 {
112-
return errors.New("max_hours cannot be negative")
112+
if v == 0 {
113+
return errors.New("value cannot be zero")
113114
}
114115

115-
if v == 0 {
116-
return errors.New("max_hours cannot be zero")
116+
if v < 0 {
117+
return errors.New("value cannot be negative")
117118
}
118119

119120
return nil
120121
}
121122

122123
// validateMinHours ensures minHours is a positive non-zero integer.
123124
func validateMinHours(v int64) error {
124-
if v < 0 {
125-
return errors.New("min_hours cannot be negative")
125+
if v == 0 {
126+
return errors.New("value cannot be zero")
126127
}
127128

128-
if v == 0 {
129-
return errors.New("min_hours cannot be zero")
129+
if v < 0 {
130+
return errors.New("value cannot be negative")
130131
}
131132

132133
return nil
@@ -141,28 +142,28 @@ func validateProofVerificationEnabled(v bool) error {
141142
// validateStakingShare ensures stakingShare is not nil, not negative, and ≤ 1.
142143
func validateStakingShare(v math.LegacyDec) error {
143144
if v.IsNil() {
144-
return errors.New("staking_share cannot be nil")
145+
return errors.New("value cannot be nil")
145146
}
146147

147148
if v.IsNegative() {
148-
return errors.New("staking_share cannot be negative")
149+
return errors.New("value cannot be negative")
149150
}
150151

151152
if v.GT(math.LegacyOneDec()) {
152-
return errors.New("staking_share cannot be greater than 1")
153+
return errors.New("value cannot be greater than 1")
153154
}
154155

155156
return nil
156157
}
157158

158159
// validateStatusChangeDelay ensures the delay is positive and non-zero.
159160
func validateStatusChangeDelay(v time.Duration) error {
160-
if v < 0 {
161-
return errors.New("status_change_delay cannot be negative")
161+
if v == 0 {
162+
return errors.New("value cannot be zero")
162163
}
163164

164-
if v == 0 {
165-
return errors.New("status_change_delay cannot be zero")
165+
if v < 0 {
166+
return errors.New("value cannot be negative")
166167
}
167168

168169
return nil

0 commit comments

Comments
 (0)