From af625344a5e52e9a533f66c5a7670ecbacb4b36a Mon Sep 17 00:00:00 2001 From: Gibheer Date: Sat, 15 May 2021 21:18:19 +0200 Subject: [PATCH] add more detials to parsing errors --- types/ip.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/types/ip.go b/types/ip.go index 8756d0c..1844d15 100644 --- a/types/ip.go +++ b/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