Skip to content

Commit 20c509a

Browse files
committed
ref: errors
1 parent e31ca44 commit 20c509a

File tree

11 files changed

+111
-44
lines changed

11 files changed

+111
-44
lines changed

bik/bik.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ import (
1010
// example valid format is 044525225
1111
func Validate(bik string) (bool, error) {
1212
if len(bik) != 9 {
13-
return false, ru_doc_code.ErrInvalidBIKLength
13+
pkg, err := ru_doc_code.GetPackageName()
14+
if err != nil {
15+
return false, err
16+
}
17+
return false, &ru_doc_code.CommonError{
18+
Method: pkg,
19+
Err: ru_doc_code.ErrInvalidLength,
20+
}
1421
}
1522

1623
bikArr, err := ru_doc_code.StrToArr(bik)

bik/bik_test.go

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

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

78
"github.com/stretchr/testify/assert"
@@ -16,12 +17,12 @@ func TestValidate(t *testing.T) {
1617
testCases := []ru_doc_code.TestCodeCase{
1718
{
1819
Code: "1234567888776",
19-
Error: ru_doc_code.ErrInvalidBIKLength,
20+
Error: ru_doc_code.ErrInvalidLength,
2021
IsValid: false,
2122
},
2223
{
2324
Code: "044525",
24-
Error: ru_doc_code.ErrInvalidBIKLength,
25+
Error: ru_doc_code.ErrInvalidLength,
2526
IsValid: false,
2627
},
2728
{
@@ -35,10 +36,14 @@ func TestValidate(t *testing.T) {
3536
IsValid: true,
3637
},
3738
}
38-
for _, test := range testCases {
39+
for i, test := range testCases {
3940
isValid, err := Validate(test.Code)
4041
assert.Equal(t, test.IsValid, isValid, test.Code)
41-
assert.Equal(t, true, errors.Is(test.Error, err), test.Code)
42+
if err != nil {
43+
assert.True(t, errors.As(err, &test.Error), fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
44+
} else {
45+
assert.Empty(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
46+
}
4247
}
4348
})
4449

@@ -75,10 +80,14 @@ func TestValidate(t *testing.T) {
7580
IsValid: true,
7681
},
7782
}
78-
for _, test := range testCases {
83+
for i, test := range testCases {
7984
isValid, err := Validate(test.Code)
8085
assert.Equal(t, test.IsValid, isValid, test.Code, test.IsValid)
81-
assert.Equal(t, true, errors.Is(test.Error, err), test.Code)
86+
if err != nil {
87+
assert.True(t, errors.As(err, &test.Error), fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
88+
} else {
89+
assert.Empty(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
90+
}
8291
}
8392
})
8493
}

errors.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@ package ru_doc_code
33
import (
44
"errors"
55
"fmt"
6+
"path/filepath"
67
"runtime"
78
"strings"
89
)
910

10-
const (
11-
unixLikePathSep = "/"
12-
windowsPathSep = "\\"
13-
)
14-
1511
var (
1612
// ErrInvalidLength invalid input document code length
1713
ErrInvalidLength = errors.New("invalid length")
@@ -49,12 +45,7 @@ func GetPackageName() (string, error) {
4945
parts := strings.Split(runtime.FuncForPC(pc).Name(), ".")
5046
pl := len(parts)
5147

52-
sep := unixLikePathSep
53-
if runtime.GOOS == "windows" {
54-
sep = windowsPathSep
55-
}
56-
57-
pathArr := strings.Split(parts[pl-2], sep)
48+
pathArr := strings.Split(parts[pl-2], string(filepath.Separator))
5849
if len(pathArr) == 0 {
5950
return "", errors.New("invalid path length")
6051
}

inn/inn.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@ type INN struct {
2424
// example: input format is 7707083893
2525
func Validate(inn string) (bool, error) {
2626
if len(inn) != lengthLegal && len(inn) != lengthPhysical {
27-
return false, ru_doc_code.ErrInvalidINNLength
27+
pkg, err := ru_doc_code.GetPackageName()
28+
if err != nil {
29+
return false, err
30+
}
31+
return false, &ru_doc_code.CommonError{
32+
Method: pkg,
33+
Err: ru_doc_code.ErrInvalidLength,
34+
}
2835
}
2936

3037
innArr, err := transformInn(inn)

inn/inn_test.go

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

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

78
"github.com/stretchr/testify/assert"
@@ -17,12 +18,12 @@ func TestValidate(t *testing.T) {
1718
testCases := []ru_doc_code.TestCodeCase{
1819
{
1920
Code: "12345678",
20-
Error: ru_doc_code.ErrInvalidINNLength,
21+
Error: ru_doc_code.ErrInvalidLength,
2122
IsValid: false,
2223
},
2324
{
2425
Code: "9876543211123",
25-
Error: ru_doc_code.ErrInvalidINNLength,
26+
Error: ru_doc_code.ErrInvalidLength,
2627
IsValid: false,
2728
},
2829
{
@@ -36,10 +37,14 @@ func TestValidate(t *testing.T) {
3637
IsValid: true,
3738
},
3839
}
39-
for _, test := range testCases {
40+
for i, test := range testCases {
4041
isValid, err := Validate(test.Code)
4142
assert.Equal(t, test.IsValid, isValid, test.Code)
42-
assert.Equal(t, true, errors.Is(test.Error, err), test.Code)
43+
if err != nil {
44+
assert.True(t, errors.As(err, &test.Error), fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
45+
} else {
46+
assert.Empty(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
47+
}
4348
}
4449
})
4550

@@ -71,10 +76,14 @@ func TestValidate(t *testing.T) {
7176
IsValid: true,
7277
},
7378
}
74-
for _, test := range testCases {
79+
for i, test := range testCases {
7580
isValid, err := Validate(test.Code)
7681
assert.Equal(t, test.IsValid, isValid, test.Code)
77-
assert.Equal(t, true, errors.Is(test.Error, err), test.Code)
82+
if err != nil {
83+
assert.True(t, errors.As(err, &test.Error), fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
84+
} else {
85+
assert.Empty(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
86+
}
7887
}
7988
})
8089
}

ogrn/ogrn.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ import (
1010
// example: input format is 1027700132195
1111
func Validate(ogrn string) (bool, error) {
1212
if len(ogrn) != 13 {
13-
return false, ru_doc_code.ErrInvalidOGRNLength
13+
pkg, err := ru_doc_code.GetPackageName()
14+
if err != nil {
15+
return false, err
16+
}
17+
return false, &ru_doc_code.CommonError{
18+
Method: pkg,
19+
Err: ru_doc_code.ErrInvalidLength,
20+
}
1421
}
1522

1623
ogrnArr, err := ru_doc_code.StrToArr(ogrn)

ogrn/ogrn_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ogrn
22

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

78
"github.com/stretchr/testify/assert"
@@ -26,19 +27,23 @@ func TestValidate(t *testing.T) {
2627
},
2728
{
2829
Code: "102773924",
29-
Error: ru_doc_code.ErrInvalidOGRNLength,
30+
Error: ru_doc_code.ErrInvalidLength,
3031
IsValid: false,
3132
},
3233
{
3334
Code: "10277392447411231",
34-
Error: ru_doc_code.ErrInvalidOGRNLength,
35+
Error: ru_doc_code.ErrInvalidLength,
3536
IsValid: false,
3637
},
3738
}
38-
for _, test := range testCases {
39+
for i, test := range testCases {
3940
isValid, err := Validate(test.Code)
4041
assert.Equal(t, test.IsValid, isValid, test.Code)
41-
assert.Equal(t, true, errors.Is(test.Error, err), test.Code)
42+
if err != nil {
43+
assert.True(t, errors.As(err, &test.Error), fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
44+
} else {
45+
assert.Empty(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
46+
}
4247
}
4348
})
4449

ogrnip/ogrnip.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ import (
1010
// example: input format is 304500116000157
1111
func Validate(ogrnip string) (bool, error) {
1212
if len(ogrnip) != 15 {
13-
return false, ru_doc_code.ErrInvalidOGRNIPLength
13+
pkg, err := ru_doc_code.GetPackageName()
14+
if err != nil {
15+
return false, err
16+
}
17+
return false, &ru_doc_code.CommonError{
18+
Method: pkg,
19+
Err: ru_doc_code.ErrInvalidLength,
20+
}
1421
}
1522

1623
ogrnipArr, err := ru_doc_code.StrToArr(ogrnip)

ogrnip/ogrnip_test.go

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

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

78
"github.com/stretchr/testify/assert"
@@ -26,19 +27,23 @@ func TestValidate(t *testing.T) {
2627
},
2728
{
2829
Code: "31250290460",
29-
Error: ru_doc_code.ErrInvalidOGRNIPLength,
30+
Error: ru_doc_code.ErrInvalidLength,
3031
IsValid: false,
3132
},
3233
{
3334
Code: "3045001111236000157",
34-
Error: ru_doc_code.ErrInvalidOGRNIPLength,
35+
Error: ru_doc_code.ErrInvalidLength,
3536
IsValid: false,
3637
},
3738
}
38-
for _, test := range testCases {
39+
for i, test := range testCases {
3940
isValid, err := Validate(test.Code)
4041
assert.Equal(t, test.IsValid, isValid, test.Code)
41-
assert.Equal(t, true, errors.Is(test.Error, err), test.Code)
42+
if err != nil {
43+
assert.True(t, errors.As(err, &test.Error), fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
44+
} else {
45+
assert.Empty(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
46+
}
4247
}
4348
})
4449

@@ -70,10 +75,14 @@ func TestValidate(t *testing.T) {
7075
IsValid: true,
7176
},
7277
}
73-
for _, test := range testCases {
78+
for i, test := range testCases {
7479
isValid, err := Validate(test.Code)
7580
assert.Equal(t, test.IsValid, isValid, test.Code, test.IsValid)
76-
assert.Equal(t, true, errors.Is(test.Error, err), test.Code)
81+
if err != nil {
82+
assert.True(t, errors.As(err, &test.Error), fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
83+
} else {
84+
assert.Empty(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
85+
}
7786
}
7887
})
7988
}

snils/snils.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ import (
1111
// example: input format is 112-233-445 95
1212
func Validate(snils string) (bool, error) {
1313
if len(snils) != 14 {
14-
return false, ru_doc_code.ErrInvalidSNILSLength
14+
pkg, err := ru_doc_code.GetPackageName()
15+
if err != nil {
16+
return false, err
17+
}
18+
return false, &ru_doc_code.CommonError{
19+
Method: pkg,
20+
Err: ru_doc_code.ErrInvalidLength,
21+
}
1522
}
1623

1724
fSnils := strings.ReplaceAll(snils, "-", "")

0 commit comments

Comments
 (0)