Skip to content

proposal: net/netip: normalising errors returned by ParseAddrPort #65800

@aimuz

Description

@aimuz

Proposal Details

The current ParseAddrPort error has the following value

errors.New("not an ip:port")
errors.New("no IP")
errors.New("no port")
errors.New("missing ]")
errors.New("invalid port " + strconv.Quote(port) + " parsing " + strconv.Quote(s))
errors.New("invalid ip:port " + strconv.Quote(s) + ", square brackets can only be used with IPv6 addresses")
errors.New("invalid ip:port " + strconv.Quote(s) + ", IPv6 addresses must be surrounded by square brackets")

I would suggest turning these error values into the following

netip.ParseAddrPort(strconv.Quote(s)): not an ip:port
netip.ParseAddrPort(strconv.Quote(s)): no IP
netip.ParseAddrPort(strconv.Quote(s)): no port
netip.ParseAddrPort(strconv.Quote(s)): missing ]
netip.ParseAddrPort(strconv.Quote(s)): invalid port strconv.Quote(port)
netip.ParseAddrPort(strconv.Quote(s)): square brackets can only be used with IPv6 addresses
netip.ParseAddrPort(strconv.Quote(s)): IPv6 addresses must be surrounded by square brackets

A new error type can be created without exposing the

type parseAddrError struct {
	in  string // the string given to ParseAddrPort
	msg string // an explanation of the parse failure
}

func (err parseAddrError) Error() string {
	q := strconv.Quote
	return "netip.ParseAddrPort(" + q(err.in) + "): " + err.msg
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    ProposalWaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions