add more detials to parsing errors
This commit is contained in:
parent
5bb473404f
commit
af625344a5
15
types/ip.go
15
types/ip.go
|
@ -23,10 +23,10 @@ func (s *Subnet) UnmarshalJSON(in []byte) error {
|
|||
|
||||
ip, ipnet, err := net.ParseCIDR(string(in))
|
||||
if err != nil {
|
||||
return fmt.Errorf("not a valid subnet")
|
||||
return fmt.Errorf("not a valid subnet: %#v", err)
|
||||
}
|
||||
if !ipnet.IP.Equal(ip) {
|
||||
return fmt.Errorf("provided IP is not a subnet")
|
||||
return fmt.Errorf("provided IP '%s' is not network address '%s' of declared subnet", ip.String(), ipnet.IP.String())
|
||||
}
|
||||
*s = Subnet(*ipnet)
|
||||
return nil
|
||||
|
@ -40,6 +40,17 @@ func (s *Subnet) String() string {
|
|||
return (*net.IPNet)(s).String()
|
||||
}
|
||||
|
||||
func (i IP) Is4() bool {
|
||||
if a := net.IP(i).To4(); a == nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (i IP) Is6() bool {
|
||||
return !i.Is4()
|
||||
}
|
||||
|
||||
// Value implements the database Value interface.
|
||||
//
|
||||
// This function is needed so that a subnet can be inserted into
|
||||
|
|
Loading…
Reference in New Issue