Skip to content

Commit

Permalink
netutil: imp code, errors
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Feb 17, 2023
1 parent d438e02 commit ce2b431
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
6 changes: 1 addition & 5 deletions netutil/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ const MaxServiceLabelLen = 16
func ValidateServiceNameLabel(label string) (err error) {
defer makeLabelError(&err, label, LabelKindSRV)

if label == "" {
if label == "" || label == "_" {
return ErrLabelIsEmpty
} else if r := rune(label[0]); r != '_' {
return &RuneError{
Expand Down Expand Up @@ -432,10 +432,6 @@ func ValidateSRVDomainName(name string) (err error) {
err = ValidateServiceNameLabel(l)
} else {
err = ValidateHostnameLabel(l)
if err != nil {
replaceKind(err, LabelKindSRV)
replaceKind(err.(*LabelError).Err, LabelKindSRV)
}
}
if err != nil {
return err
Expand Down
8 changes: 7 additions & 1 deletion netutil/addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func TestValidateSRVDomainName(t *testing.T) {
in: "example..com",
wantErrAs: new(errors.Error),
wantErrMsg: `bad service domain name "example..com": ` +
`bad service name label "": label is empty`,
`bad hostname label "": label is empty`,
}, {
name: "bad_label_first_symbol",
in: "example._-a.com",
Expand All @@ -480,6 +480,12 @@ func TestValidateSRVDomainName(t *testing.T) {
wantErrAs: new(*netutil.AddrError),
wantErrMsg: `bad service domain name "example._.com": ` +
`bad service name label "_": label is empty`,
}, {
name: "bad_hostname_label",
in: "-srv.com",
wantErrAs: new(*netutil.AddrError),
wantErrMsg: `bad service domain name "-srv.com": ` +
`bad hostname label "-srv": bad hostname label rune '-'`,
}}

for _, tc := range testCases {
Expand Down
10 changes: 6 additions & 4 deletions netutil/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const (
ErrNotAReversedSubnet errors.Error = "not a reversed ip network"
)

// AddrKind is the kind of address or address part used for error reporting.
// AddrKind is a convenient alias for string describing the kind of address and
// used for error reporting.
type AddrKind = string

// Kinds of addresses for [AddrError].
Expand Down Expand Up @@ -97,7 +98,8 @@ func makeAddrError(errPtr *error, addr string, k AddrKind) {
}
}

// LabelKind is the kind of label of a name used for error reporting.
// LabelKind is a convenient alias for string describing the kind of label of a
// name and used for error reporting.
type LabelKind = string

// Kinds of labels for [LabelError].
Expand Down Expand Up @@ -208,7 +210,7 @@ func (err *RuneError) Error() (msg string) {
return fmt.Sprintf("bad %s rune %q", err.Kind, err.Rune)
}

// replaceKind replaces the Kind field of err with newKind if err is
// replaceKind replaces the Kind field of err with newKind. err must be of type
// [*LabelError], [*AddrError], [*LengthError], or [*RuneError].
func replaceKind(err error, newKind string) {
switch err := err.(type) {
Expand All @@ -221,6 +223,6 @@ func replaceKind(err error, newKind string) {
case *RuneError:
err.Kind = newKind
default:
// Go on.
panic(fmt.Errorf("unexpected error type %T", err))
}
}

0 comments on commit ce2b431

Please sign in to comment.