chore(errors): review all errors in codebase
This commit is contained in:
@@ -17,12 +17,12 @@ func (e *Extractor) Data(filepath string) (lines []string,
|
||||
connection models.Connection, err error) {
|
||||
lines, err = readCustomConfigLines(filepath)
|
||||
if err != nil {
|
||||
return nil, connection, fmt.Errorf("%w: %s", ErrRead, err)
|
||||
return nil, connection, fmt.Errorf("cannot read configuration file: %w", err)
|
||||
}
|
||||
|
||||
connection, err = extractDataFromLines(lines)
|
||||
if err != nil {
|
||||
return nil, connection, fmt.Errorf("%w: %s", ErrExtractConnection, err)
|
||||
return nil, connection, fmt.Errorf("cannot extract connection from file: %w", err)
|
||||
}
|
||||
|
||||
return lines, connection, nil
|
||||
|
||||
@@ -48,25 +48,20 @@ func extractDataFromLines(lines []string) (
|
||||
return connection, nil
|
||||
}
|
||||
|
||||
var (
|
||||
errExtractProto = errors.New("failed extracting protocol from proto line")
|
||||
errExtractRemote = errors.New("failed extracting from remote line")
|
||||
)
|
||||
|
||||
func extractDataFromLine(line string) (
|
||||
ip net.IP, port uint16, protocol string, err error) {
|
||||
switch {
|
||||
case strings.HasPrefix(line, "proto "):
|
||||
protocol, err = extractProto(line)
|
||||
if err != nil {
|
||||
return nil, 0, "", fmt.Errorf("%w: %s", errExtractProto, err)
|
||||
return nil, 0, "", fmt.Errorf("failed extracting protocol from proto line: %w", err)
|
||||
}
|
||||
return nil, 0, protocol, nil
|
||||
|
||||
case strings.HasPrefix(line, "remote "):
|
||||
ip, port, protocol, err = extractRemote(line)
|
||||
if err != nil {
|
||||
return nil, 0, "", fmt.Errorf("%w: %s", errExtractRemote, err)
|
||||
return nil, 0, "", fmt.Errorf("failed extracting from remote line: %w", err)
|
||||
}
|
||||
return ip, port, protocol, nil
|
||||
}
|
||||
@@ -122,7 +117,7 @@ func extractRemote(line string) (ip net.IP, port uint16,
|
||||
if err != nil {
|
||||
return nil, 0, "", fmt.Errorf("%w: %s", errPortNotValid, line)
|
||||
} else if portInt < 1 || portInt > 65535 {
|
||||
return nil, 0, "", fmt.Errorf("%w: not between 1 and 65535: %d", errPortNotValid, portInt)
|
||||
return nil, 0, "", fmt.Errorf("%w: %d must be between 1 and 65535", errPortNotValid, portInt)
|
||||
}
|
||||
port = uint16(portInt)
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ func Test_extractDataFromLine(t *testing.T) {
|
||||
},
|
||||
"extract proto error": {
|
||||
line: "proto bad",
|
||||
isErr: errExtractProto,
|
||||
isErr: errProtocolNotSupported,
|
||||
},
|
||||
"extract proto success": {
|
||||
line: "proto tcp",
|
||||
@@ -106,7 +106,7 @@ func Test_extractDataFromLine(t *testing.T) {
|
||||
},
|
||||
"extract remote error": {
|
||||
line: "remote bad",
|
||||
isErr: errExtractRemote,
|
||||
isErr: errHostNotIP,
|
||||
},
|
||||
"extract remote success": {
|
||||
line: "remote 1.2.3.4 1194 udp",
|
||||
@@ -213,15 +213,15 @@ func Test_extractRemote(t *testing.T) {
|
||||
},
|
||||
"port is zero": {
|
||||
line: "remote 1.2.3.4 0",
|
||||
err: errors.New("port is not valid: not between 1 and 65535: 0"),
|
||||
err: errors.New("port is not valid: 0 must be between 1 and 65535"),
|
||||
},
|
||||
"port is minus one": {
|
||||
line: "remote 1.2.3.4 -1",
|
||||
err: errors.New("port is not valid: not between 1 and 65535: -1"),
|
||||
err: errors.New("port is not valid: -1 must be between 1 and 65535"),
|
||||
},
|
||||
"port is over 65535": {
|
||||
line: "remote 1.2.3.4 65536",
|
||||
err: errors.New("port is not valid: not between 1 and 65535: 65536"),
|
||||
err: errors.New("port is not valid: 65536 must be between 1 and 65535"),
|
||||
},
|
||||
"IP host and port": {
|
||||
line: "remote 1.2.3.4 8000",
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
func ExtractCert(b []byte) (certData string, err error) {
|
||||
certData, err = extractPEM(b, "CERTIFICATE")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%w: %s", ErrExtractPEM, err)
|
||||
return "", fmt.Errorf("cannot extract PEM data: %w", err)
|
||||
}
|
||||
|
||||
return certData, nil
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package parse
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrExtractPEM = errors.New("cannot extract PEM data")
|
||||
)
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
func ExtractPrivateKey(b []byte) (keyData string, err error) {
|
||||
keyData, err = extractPEM(b, "PRIVATE KEY")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%w: %s", ErrExtractPEM, err)
|
||||
return "", fmt.Errorf("cannot extract PEM data: %w", err)
|
||||
}
|
||||
|
||||
return keyData, nil
|
||||
|
||||
Reference in New Issue
Block a user