add more detials to parsing errors

This commit is contained in:
Gibheer 2021-05-15 21:18:19 +02:00
parent 5bb473404f
commit af625344a5
1 changed files with 13 additions and 2 deletions

View File

@ -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