Skip to content

Commit 5d1e1f6

Browse files
refactor(rule): change error message
1 parent ce0cb98 commit 5d1e1f6

File tree

7 files changed

+115
-39
lines changed

7 files changed

+115
-39
lines changed

rule/charset.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ func (r *ScopeCharsetRule) Validate(msg *lint.Commit) (string, bool) {
2727

2828
// Apply sets the needed argument for the rule
2929
func (r *ScopeCharsetRule) Apply(arg interface{}, flags map[string]interface{}) error {
30-
return setStringArg(&r.Charset, arg, r.Name())
30+
err := setStringArg(&r.Charset, arg)
31+
if err != nil {
32+
return errInvalidArg(r.Name(), err)
33+
}
34+
return nil
3135
}
3236

3337
// TypeCharsetRule to validate max length of header
@@ -50,7 +54,11 @@ func (r *TypeCharsetRule) Validate(msg *lint.Commit) (string, bool) {
5054

5155
// Apply sets the needed argument for the rule
5256
func (r *TypeCharsetRule) Apply(arg interface{}, flags map[string]interface{}) error {
53-
return setStringArg(&r.Charset, arg, r.Name())
57+
err := setStringArg(&r.Charset, arg)
58+
if err != nil {
59+
return errInvalidArg(r.Name(), err)
60+
}
61+
return nil
5462
}
5563

5664
func checkCharset(charset, toCheck string) (string, bool) {

rule/enum.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,18 @@ func (r *ScopeEnumRule) Validate(msg *lint.Commit) (string, bool) {
3737

3838
// Apply sets the needed argument for the rule
3939
func (r *ScopeEnumRule) Apply(arg interface{}, flags map[string]interface{}) error {
40-
err := setStringArrArg(&r.Scopes, arg, r.Name())
40+
err := setStringArrArg(&r.Scopes, arg)
4141
if err != nil {
42-
return err
42+
return errInvalidArg(r.Name(), err)
4343
}
4444

4545
allowEmpty, ok := flags["allow-empty"]
4646
if ok {
47-
err := setBoolArg(&r.AllowEmpty, allowEmpty, r.Name())
47+
err := setBoolArg(&r.AllowEmpty, allowEmpty)
4848
if err != nil {
49-
return err
49+
return errInvalidFlag(r.Name(), "allow-empty", err)
5050
}
51+
return nil
5152
}
5253

5354
// sorting the string elements for binary search
@@ -75,9 +76,9 @@ func (r *TypeEnumRule) Validate(msg *lint.Commit) (string, bool) {
7576

7677
// Apply sets the needed argument for the rule
7778
func (r *TypeEnumRule) Apply(arg interface{}, flags map[string]interface{}) error {
78-
err := setStringArrArg(&r.Types, arg, r.Name())
79+
err := setStringArrArg(&r.Types, arg)
7980
if err != nil {
80-
return err
81+
return errInvalidArg(r.Name(), err)
8182
}
8283
// sorting the string elements for binary search
8384
sort.Strings(r.Types)

rule/errors.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package rule
2+
3+
import "fmt"
4+
5+
func errInvalidArg(ruleName string, err error) error {
6+
return fmt.Errorf("%s: invalid argument: %v", ruleName, err)
7+
}
8+
9+
func errInvalidFlag(ruleName, flagName string, err error) error {
10+
return fmt.Errorf("%s: invalid flag '%s': %v", ruleName, flagName, err)
11+
}

rule/max_length.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ func (r *HeadMaxLenRule) Validate(msg *lint.Commit) (string, bool) {
2121

2222
// Apply sets the needed argument for the rule
2323
func (r *HeadMaxLenRule) Apply(arg interface{}, flags map[string]interface{}) error {
24-
return setIntArg(&r.CheckLen, arg, r.Name())
24+
err := setIntArg(&r.CheckLen, arg)
25+
if err != nil {
26+
return errInvalidArg(r.Name(), err)
27+
}
28+
return nil
2529
}
2630

2731
// BodyMaxLenRule to validate max length of body
@@ -39,7 +43,11 @@ func (r *BodyMaxLenRule) Validate(msg *lint.Commit) (string, bool) {
3943

4044
// Apply sets the needed argument for the rule
4145
func (r *BodyMaxLenRule) Apply(arg interface{}, flags map[string]interface{}) error {
42-
return setIntArg(&r.CheckLen, arg, r.Name())
46+
err := setIntArg(&r.CheckLen, arg)
47+
if err != nil {
48+
return errInvalidArg(r.Name(), err)
49+
}
50+
return nil
4351
}
4452

4553
// FooterMaxLenRule to validate max length of footer
@@ -57,7 +65,11 @@ func (r *FooterMaxLenRule) Validate(msg *lint.Commit) (string, bool) {
5765

5866
// Apply sets the needed argument for the rule
5967
func (r *FooterMaxLenRule) Apply(arg interface{}, flags map[string]interface{}) error {
60-
return setIntArg(&r.CheckLen, arg, r.Name())
68+
err := setIntArg(&r.CheckLen, arg)
69+
if err != nil {
70+
return errInvalidArg(r.Name(), err)
71+
}
72+
return nil
6173
}
6274

6375
// TypeMaxLenRule to validate max length of type
@@ -75,7 +87,11 @@ func (r *TypeMaxLenRule) Validate(msg *lint.Commit) (string, bool) {
7587

7688
// Apply sets the needed argument for the rule
7789
func (r *TypeMaxLenRule) Apply(arg interface{}, flags map[string]interface{}) error {
78-
return setIntArg(&r.CheckLen, arg, r.Name())
90+
err := setIntArg(&r.CheckLen, arg)
91+
if err != nil {
92+
return errInvalidArg(r.Name(), err)
93+
}
94+
return nil
7995
}
8096

8197
// ScopeMaxLenRule to validate max length of type
@@ -93,7 +109,11 @@ func (r *ScopeMaxLenRule) Validate(msg *lint.Commit) (string, bool) {
93109

94110
// Apply sets the needed argument for the rule
95111
func (r *ScopeMaxLenRule) Apply(arg interface{}, flags map[string]interface{}) error {
96-
return setIntArg(&r.CheckLen, arg, r.Name())
112+
err := setIntArg(&r.CheckLen, arg)
113+
if err != nil {
114+
return errInvalidArg(r.Name(), err)
115+
}
116+
return nil
97117
}
98118

99119
// DescriptionMaxLenRule to validate max length of type
@@ -111,7 +131,11 @@ func (r *DescriptionMaxLenRule) Validate(msg *lint.Commit) (string, bool) {
111131

112132
// Apply sets the needed argument for the rule
113133
func (r *DescriptionMaxLenRule) Apply(arg interface{}, flags map[string]interface{}) error {
114-
return setIntArg(&r.CheckLen, arg, r.Name())
134+
err := setIntArg(&r.CheckLen, arg)
135+
if err != nil {
136+
return errInvalidArg(r.Name(), err)
137+
}
138+
return nil
115139
}
116140

117141
func checkMaxLen(checkLen int, toCheck string) (string, bool) {

rule/max_line_length.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ func (r *BodyMaxLineLenRule) Validate(msg *lint.Commit) (string, bool) {
2222

2323
// Apply sets the needed argument for the rule
2424
func (r *BodyMaxLineLenRule) Apply(arg interface{}, flags map[string]interface{}) error {
25-
return setIntArg(&r.CheckLen, arg, r.Name())
25+
err := setIntArg(&r.CheckLen, arg)
26+
if err != nil {
27+
return errInvalidArg(r.Name(), err)
28+
}
29+
return nil
2630
}
2731

2832
// FooterMaxLineLenRule to validate max line length of footer
@@ -40,7 +44,11 @@ func (r *FooterMaxLineLenRule) Validate(msg *lint.Commit) (string, bool) {
4044

4145
// Apply sets the needed argument for the rule
4246
func (r *FooterMaxLineLenRule) Apply(arg interface{}, flags map[string]interface{}) error {
43-
return setIntArg(&r.CheckLen, arg, r.Name())
47+
err := setIntArg(&r.CheckLen, arg)
48+
if err != nil {
49+
return errInvalidArg(r.Name(), err)
50+
}
51+
return nil
4452
}
4553

4654
func checkMaxLineLength(checkLen int, toCheck string) (string, bool) {

rule/min_length.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ func (r *HeadMinLenRule) Validate(msg *lint.Commit) (string, bool) {
2121

2222
// Apply sets the needed argument for the rule
2323
func (r *HeadMinLenRule) Apply(arg interface{}, flags map[string]interface{}) error {
24-
return setIntArg(&r.CheckLen, arg, r.Name())
24+
err := setIntArg(&r.CheckLen, arg)
25+
if err != nil {
26+
return errInvalidArg(r.Name(), err)
27+
}
28+
return nil
2529
}
2630

2731
// BodyMinLenRule to validate min length of body
@@ -39,7 +43,11 @@ func (r *BodyMinLenRule) Validate(msg *lint.Commit) (string, bool) {
3943

4044
// Apply sets the needed argument for the rule
4145
func (r *BodyMinLenRule) Apply(arg interface{}, flags map[string]interface{}) error {
42-
return setIntArg(&r.CheckLen, arg, r.Name())
46+
err := setIntArg(&r.CheckLen, arg)
47+
if err != nil {
48+
return errInvalidArg(r.Name(), err)
49+
}
50+
return nil
4351
}
4452

4553
// FooterMinLenRule to validate min length of footer
@@ -57,7 +65,11 @@ func (r *FooterMinLenRule) Validate(msg *lint.Commit) (string, bool) {
5765

5866
// Apply sets the needed argument for the rule
5967
func (r *FooterMinLenRule) Apply(arg interface{}, flags map[string]interface{}) error {
60-
return setIntArg(&r.CheckLen, arg, r.Name())
68+
err := setIntArg(&r.CheckLen, arg)
69+
if err != nil {
70+
return errInvalidArg(r.Name(), err)
71+
}
72+
return nil
6173
}
6274

6375
// TypeMinLenRule to validate min length of type
@@ -75,7 +87,11 @@ func (r *TypeMinLenRule) Validate(msg *lint.Commit) (string, bool) {
7587

7688
// Apply sets the needed argument for the rule
7789
func (r *TypeMinLenRule) Apply(arg interface{}, flags map[string]interface{}) error {
78-
return setIntArg(&r.CheckLen, arg, r.Name())
90+
err := setIntArg(&r.CheckLen, arg)
91+
if err != nil {
92+
return errInvalidArg(r.Name(), err)
93+
}
94+
return nil
7995
}
8096

8197
// ScopeMinLenRule to validate min length of scope
@@ -93,7 +109,11 @@ func (r *ScopeMinLenRule) Validate(msg *lint.Commit) (string, bool) {
93109

94110
// Apply sets the needed argument for the rule
95111
func (r *ScopeMinLenRule) Apply(arg interface{}, flags map[string]interface{}) error {
96-
return setIntArg(&r.CheckLen, arg, r.Name())
112+
err := setIntArg(&r.CheckLen, arg)
113+
if err != nil {
114+
return errInvalidArg(r.Name(), err)
115+
}
116+
return nil
97117
}
98118

99119
// DescriptionMinLenRule to validate min length of description
@@ -111,7 +131,11 @@ func (r *DescriptionMinLenRule) Validate(msg *lint.Commit) (string, bool) {
111131

112132
// Apply sets the needed argument for the rule
113133
func (r *DescriptionMinLenRule) Apply(arg interface{}, flags map[string]interface{}) error {
114-
return setIntArg(&r.CheckLen, arg, r.Name())
134+
err := setIntArg(&r.CheckLen, arg)
135+
if err != nil {
136+
return errInvalidArg(r.Name(), err)
137+
}
138+
return nil
115139
}
116140

117141
func checkMinLen(checkLen int, toCheck string) (string, bool) {

rule/util.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,80 +4,80 @@ import (
44
"fmt"
55
)
66

7-
func setBoolArg(retVal *bool, arg interface{}, ruleName string) error {
8-
boolVal, err := toBool(arg, ruleName)
7+
func setBoolArg(retVal *bool, arg interface{}) error {
8+
boolVal, err := toBool(arg)
99
if err != nil {
1010
return err
1111
}
1212
*retVal = boolVal
1313
return nil
1414
}
1515

16-
func setIntArg(retVal *int, arg interface{}, ruleName string) error {
17-
intVal, err := toInt(arg, ruleName)
16+
func setIntArg(retVal *int, arg interface{}) error {
17+
intVal, err := toInt(arg)
1818
if err != nil {
1919
return err
2020
}
2121
*retVal = intVal
2222
return nil
2323
}
2424

25-
func setStringArg(retVal *string, arg interface{}, ruleName string) error {
26-
strVal, err := toString(arg, ruleName)
25+
func setStringArg(retVal *string, arg interface{}) error {
26+
strVal, err := toString(arg)
2727
if err != nil {
2828
return err
2929
}
3030
*retVal = strVal
3131
return nil
3232
}
3333

34-
func setStringArrArg(retVal *[]string, arg interface{}, ruleName string) error {
35-
arrVal, err := toStringArr(arg, ruleName)
34+
func setStringArrArg(retVal *[]string, arg interface{}) error {
35+
arrVal, err := toStringArr(arg)
3636
if err != nil {
3737
return err
3838
}
3939
*retVal = arrVal
4040
return nil
4141
}
4242

43-
func toBool(arg interface{}, ruleName string) (bool, error) {
43+
func toBool(arg interface{}) (bool, error) {
4444
boolVal, ok := arg.(bool)
4545
if !ok {
46-
return false, fmt.Errorf("%s expects bool value, but got %#v", ruleName, arg)
46+
return false, fmt.Errorf("expects bool value, but got %#v", arg)
4747
}
4848
return boolVal, nil
4949
}
5050

51-
func toInt(arg interface{}, ruleName string) (int, error) {
51+
func toInt(arg interface{}) (int, error) {
5252
intVal, ok := arg.(int)
5353
if !ok {
54-
return 0, fmt.Errorf("%s expects int value, but got %#v", ruleName, arg)
54+
return 0, fmt.Errorf("expects int value, but got %#v", arg)
5555
}
5656
return intVal, nil
5757
}
5858

59-
func toString(arg interface{}, ruleName string) (string, error) {
59+
func toString(arg interface{}) (string, error) {
6060
strVal, ok := arg.(string)
6161
if !ok {
62-
return "", fmt.Errorf("%s expects string value, but got %#v", ruleName, arg)
62+
return "", fmt.Errorf("expects string value, but got %#v", arg)
6363
}
6464
return strVal, nil
6565
}
6666

67-
func toStringArr(arg interface{}, ruleName string) ([]string, error) {
67+
func toStringArr(arg interface{}) ([]string, error) {
6868
switch argVal := arg.(type) {
6969
case []interface{}:
7070
strArr := make([]string, len(argVal))
7171
for index, a := range argVal {
7272
strVal, ok := a.(string)
7373
if !ok {
74-
return nil, fmt.Errorf("%s expects array of string value, but got %#v", ruleName, arg)
74+
return nil, fmt.Errorf("expects array of string value, but got %#v", arg)
7575
}
7676
strArr[index] = strVal
7777
}
7878
return strArr, nil
7979
case []string:
8080
return argVal, nil
8181
}
82-
return nil, fmt.Errorf("%s expects array of string value, but got %#v", ruleName, arg)
82+
return nil, fmt.Errorf("expects array of string value, but got %#v", arg)
8383
}

0 commit comments

Comments
 (0)