Skip to content

Commit 09225f6

Browse files
authored
Merge pull request #2 from mrfoe7/change-errors
ref: change errors interfaces
2 parents b5be9f2 + 6d744e3 commit 09225f6

File tree

17 files changed

+212
-100
lines changed

17 files changed

+212
-100
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+
name, err := ru_doc_code.GetModuleName()
14+
if err != nil {
15+
return false, err
16+
}
17+
return false, &ru_doc_code.CommonError{
18+
Method: name,
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: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,57 @@
11
package ru_doc_code
22

3-
import "errors"
3+
import (
4+
"errors"
5+
"fmt"
6+
"path/filepath"
7+
"runtime"
8+
"strings"
9+
)
410

511
var (
6-
// ErrInvalidINNLength
7-
ErrInvalidINNLength = errors.New("invalid inn length")
8-
9-
// ErrInvalidBIKLength
10-
ErrInvalidBIKLength = errors.New("invalid bik length")
11-
12-
// ErrInvalidKPPLength
13-
ErrInvalidKPPLength = errors.New("invalid kpp length")
14-
15-
// ErrInvalidOGRNLength
16-
ErrInvalidOGRNLength = errors.New("invalid ogrn length")
12+
// ErrInvalidLength invalid input document code length
13+
ErrInvalidLength = errors.New("invalid length")
1714

18-
// ErrInvalidOGRNIPLength
19-
ErrInvalidOGRNIPLength = errors.New("invalid ogrinp length")
20-
21-
// ErrInvalidSNILSLength
22-
ErrInvalidSNILSLength = errors.New("invalid snils length")
23-
24-
// ErrInvalidFormattedSNILSLength
15+
// ErrInvalidFormattedSNILSLength invalid formatted length of snils
2516
ErrInvalidFormattedSNILSLength = errors.New("invalid formatted snils length")
2617

27-
// ErrInvalidRegistrationReasonCode
28-
ErrInvalidRegistrationReasonCode = errors.New("invalid registration reason code")
18+
// ErrRegistrationReasonCode invalid registration reason code
19+
ErrRegistrationReasonCode = errors.New("invalid registration reason code")
2920

30-
// ErrInvalidValue
21+
// ErrInvalidValue invalid input value
3122
ErrInvalidValue = errors.New("invalid code value")
3223

33-
// ErrInvalidBIKCountryCode
24+
// ErrInvalidBIKCountryCode invalid bik code country
3425
ErrInvalidBIKCountryCode = errors.New("invalid bik country code")
3526

36-
// ErrNotImplemented
27+
// ErrNotImplemented not implemented method error
3728
ErrNotImplemented = errors.New("method does not implemented")
3829
)
30+
31+
// CommonError common error wrapped base error
32+
type CommonError struct {
33+
Method string
34+
Err error
35+
}
36+
37+
func (c *CommonError) Error() string {
38+
return fmt.Sprintf("%s: %s", c.Method, c.Err.Error())
39+
}
40+
41+
// GetModuleName get package name in runtime
42+
func GetModuleName() (string, error) {
43+
pc, _, _, ok := runtime.Caller(1)
44+
if !ok {
45+
return "", errors.New("invalid runtime caller")
46+
}
47+
parts := strings.Split(runtime.FuncForPC(pc).Name(), ".")
48+
pl := len(parts)
49+
50+
pathArr := strings.Split(parts[pl-2], string(filepath.Separator))
51+
if len(pathArr) == 0 {
52+
return "", errors.New("invalid path length")
53+
}
54+
pkgName := pathArr[len(pathArr)-1]
55+
56+
return pkgName, nil
57+
}

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+
name, err := ru_doc_code.GetModuleName()
28+
if err != nil {
29+
return false, err
30+
}
31+
return false, &ru_doc_code.CommonError{
32+
Method: name,
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
}

kpp/kpp.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package kpp
22

3-
import (
4-
ru_doc_code "github.com/mrfoe7/ru-doc-code"
5-
)
3+
import ru_doc_code "github.com/mrfoe7/ru-doc-code"
64

75
type KPP struct {
86
Code ru_doc_code.TaxRegionCode
@@ -14,7 +12,14 @@ type KPP struct {
1412
// example: input format is 773643301
1513
func Validate(kpp string) (bool, error) {
1614
if len(kpp) != 9 {
17-
return false, ru_doc_code.ErrInvalidKPPLength
15+
name, err := ru_doc_code.GetModuleName()
16+
if err != nil {
17+
return false, err
18+
}
19+
return false, &ru_doc_code.CommonError{
20+
Method: name,
21+
Err: ru_doc_code.ErrInvalidLength,
22+
}
1823
}
1924

2025
_, err := ru_doc_code.StrToArr(kpp)
@@ -26,7 +31,7 @@ func Validate(kpp string) (bool, error) {
2631

2732
_, ok := ru_doc_code.SupportedRegistrationReasonSet[ru_doc_code.RegistrationReasonCode(kpp[4:6])]
2833
if !ok {
29-
return false, ru_doc_code.ErrInvalidRegistrationReasonCode
34+
return false, ru_doc_code.ErrRegistrationReasonCode
3035
}
3136

3237
return true, nil

kpp/kpp_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ func TestValidate(t *testing.T) {
1717
testCases := []ru_doc_code.TestCodeCase{
1818
{
1919
Code: "1234567888776",
20-
Error: ru_doc_code.ErrInvalidKPPLength,
20+
Error: ru_doc_code.ErrInvalidLength,
2121
IsValid: false,
2222
},
2323
{
2424
Code: "044525",
25-
Error: ru_doc_code.ErrInvalidKPPLength,
25+
Error: ru_doc_code.ErrInvalidLength,
2626
IsValid: false,
2727
},
2828
{
@@ -39,7 +39,11 @@ func TestValidate(t *testing.T) {
3939
for i, test := range testCases {
4040
isValid, err := Validate(test.Code)
4141
assert.Equal(t, isValid, test.IsValid, fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
42-
assert.Equal(t, true, errors.Is(test.Error, err), fmt.Sprintf("invalid test case %d: input: %s", i, 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+
}
4347
}
4448
})
4549

@@ -69,15 +73,19 @@ func TestValidate(t *testing.T) {
6973
for i, test := range testCases {
7074
isValid, err := Validate(test.Code)
7175
assert.Equal(t, isValid, test.IsValid, fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
72-
assert.Equal(t, true, errors.Is(test.Error, err), fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
76+
if err != nil {
77+
assert.True(t, errors.As(err, &test.Error), fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
78+
} else {
79+
assert.Empty(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
80+
}
7381
}
7482
})
7583

7684
t.Run("invalid registration reason code", func(t *testing.T) {
7785
testCases := []ru_doc_code.TestCodeCase{
7886
{
7987
Code: "773643301",
80-
Error: ru_doc_code.ErrInvalidRegistrationReasonCode,
88+
Error: ru_doc_code.ErrRegistrationReasonCode,
8189
IsValid: false,
8290
},
8391
{
@@ -89,7 +97,11 @@ func TestValidate(t *testing.T) {
8997
for i, test := range testCases {
9098
isValid, err := Validate(test.Code)
9199
assert.Equal(t, isValid, test.IsValid, fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
92-
assert.Equal(t, true, errors.Is(test.Error, err), fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
100+
if err != nil {
101+
assert.True(t, errors.As(err, &test.Error), fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
102+
} else {
103+
assert.Empty(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, test.Code))
104+
}
93105
}
94106
})
95107
}

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+
name, err := ru_doc_code.GetModuleName()
14+
if err != nil {
15+
return false, err
16+
}
17+
return false, &ru_doc_code.CommonError{
18+
Method: name,
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+
name, err := ru_doc_code.GetModuleName()
14+
if err != nil {
15+
return false, err
16+
}
17+
return false, &ru_doc_code.CommonError{
18+
Method: name,
19+
Err: ru_doc_code.ErrInvalidLength,
20+
}
1421
}
1522

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

0 commit comments

Comments
 (0)