Skip to content

Commit e31ca44

Browse files
committed
ref: errors
1 parent b5be9f2 commit e31ca44

File tree

3 files changed

+73
-28
lines changed

3 files changed

+73
-28
lines changed

errors.go

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11
package ru_doc_code
22

3-
import "errors"
4-
5-
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")
3+
import (
4+
"errors"
5+
"fmt"
6+
"runtime"
7+
"strings"
8+
)
179

18-
// ErrInvalidOGRNIPLength
19-
ErrInvalidOGRNIPLength = errors.New("invalid ogrinp length")
10+
const (
11+
unixLikePathSep = "/"
12+
windowsPathSep = "\\"
13+
)
2014

21-
// ErrInvalidSNILSLength
22-
ErrInvalidSNILSLength = errors.New("invalid snils length")
15+
var (
16+
// ErrInvalidLength invalid input document code length
17+
ErrInvalidLength = errors.New("invalid length")
2318

2419
// ErrInvalidFormattedSNILSLength
2520
ErrInvalidFormattedSNILSLength = errors.New("invalid formatted snils length")
2621

27-
// ErrInvalidRegistrationReasonCode
28-
ErrInvalidRegistrationReasonCode = errors.New("invalid registration reason code")
22+
// ErrRegistrationReasonCode
23+
ErrRegistrationReasonCode = errors.New("invalid registration reason code")
2924

3025
// ErrInvalidValue
3126
ErrInvalidValue = errors.New("invalid code value")
@@ -36,3 +31,34 @@ var (
3631
// ErrNotImplemented
3732
ErrNotImplemented = errors.New("method does not implemented")
3833
)
34+
35+
type CommonError struct {
36+
Method string
37+
Err error
38+
}
39+
40+
func (c *CommonError) Error() string {
41+
return fmt.Sprintf("%s: %s", c.Method, c.Err.Error())
42+
}
43+
44+
func GetPackageName() (string, error) {
45+
pc, _, _, ok := runtime.Caller(1)
46+
if !ok {
47+
return "", errors.New("invalid runtime caller")
48+
}
49+
parts := strings.Split(runtime.FuncForPC(pc).Name(), ".")
50+
pl := len(parts)
51+
52+
sep := unixLikePathSep
53+
if runtime.GOOS == "windows" {
54+
sep = windowsPathSep
55+
}
56+
57+
pathArr := strings.Split(parts[pl-2], sep)
58+
if len(pathArr) == 0 {
59+
return "", errors.New("invalid path length")
60+
}
61+
pkgName := pathArr[len(pathArr)-1]
62+
63+
return pkgName, nil
64+
}

kpp/kpp.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ type KPP struct {
1414
// example: input format is 773643301
1515
func Validate(kpp string) (bool, error) {
1616
if len(kpp) != 9 {
17-
return false, ru_doc_code.ErrInvalidKPPLength
17+
pkg, err := ru_doc_code.GetPackageName()
18+
if err != nil {
19+
return false, err
20+
}
21+
return false, &ru_doc_code.CommonError{
22+
Method: pkg,
23+
Err: ru_doc_code.ErrInvalidLength,
24+
}
1825
}
1926

2027
_, err := ru_doc_code.StrToArr(kpp)
@@ -26,7 +33,7 @@ func Validate(kpp string) (bool, error) {
2633

2734
_, ok := ru_doc_code.SupportedRegistrationReasonSet[ru_doc_code.RegistrationReasonCode(kpp[4:6])]
2835
if !ok {
29-
return false, ru_doc_code.ErrInvalidRegistrationReasonCode
36+
return false, ru_doc_code.ErrRegistrationReasonCode
3037
}
3138

3239
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
}

0 commit comments

Comments
 (0)