From 6701162ef328b74d69c650b5543da8176abc6781 Mon Sep 17 00:00:00 2001 From: "simon.mittag" Date: Sun, 11 Jun 2023 16:34:12 +1000 Subject: [PATCH] better error messages --- config.go | 12 ++++++------ config_test.go | 8 +++++--- stats.go | 4 ++-- upstream.go | 6 +++--- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/config.go b/config.go index cc2c38ca..0702a039 100644 --- a/config.go +++ b/config.go @@ -165,11 +165,11 @@ func (config Config) validateResources() *Config { for name := range config.Resources { resourceMappings := config.Resources[name] if len(resourceMappings) == 0 { - config.panic("resource needs to have at least one url, see https://j8a.io/docs") + config.panic(fmt.Sprintf("resource '%v' needs to have at least one url, see https://j8a.io/docs", name)) } for _, r := range resourceMappings { if r.URL.Port <= 1 || r.URL.Port > 65535 { - config.panic("resource needs to have port between 0 and 65535") + config.panic(fmt.Sprintf("resource '%v' needs to have port between 1 and 65535, was: %v", name, r.URL.Port)) } if len(r.URL.Host) == 0 { config.panic("resource needs to have host") @@ -177,15 +177,15 @@ func (config Config) validateResources() *Config { ie := validIpAddress(r) he := validHostName(r) if ie != nil && he != nil { - config.panic(fmt.Sprintf("resource host needs to be valid DNS name or IP address: %v", r.URL.Host)) + config.panic(fmt.Sprintf("resource '%v' host needs to be valid DNS name or IP address, was: %v", name, r.URL.Host)) } } - sm := "resource needs to have valid scheme: " + sm := "resource '%v' needs to have valid scheme, was: %v" if len(r.URL.Scheme) == 0 { - config.panic(sm + r.URL.Scheme) + config.panic(fmt.Sprintf(sm, name, r.URL.Scheme)) } else if !validScheme(r.URL.Scheme) { - config.panic(sm + r.URL.Scheme) + config.panic(fmt.Sprintf(sm, name, r.URL.Scheme)) } } } diff --git a/config_test.go b/config_test.go index e9cc8d68..3d719345 100644 --- a/config_test.go +++ b/config_test.go @@ -434,13 +434,15 @@ func TestResourceMappingValidUpstreamResource(t *testing.T) { n string s string h string - p uint16 + p int v bool }{ //ports + {"invalid port -1", "http", "host.com", -1, false}, {"invalid port 0", "http", "host.com", 0, false}, {"valid port", "http", "host.com", 80, true}, {"invalid port 65535", "http", "host.com", 65535, true}, + {"invalid port 65536", "http", "host.com", 65536, false}, //schemes {"valid scheme http", "Http", "host.com", 80, true}, @@ -489,7 +491,7 @@ func TestResourceMappingValidUpstreamResource(t *testing.T) { n string s string h string - p uint16 + p int v bool }{fmt.Sprintf("valid ipv4 %v", i), "http", ipv4, 80, true}) } @@ -499,7 +501,7 @@ func TestResourceMappingValidUpstreamResource(t *testing.T) { n string s string h string - p uint16 + p int v bool }{fmt.Sprintf("valid ipv6 %v", i), "https", ipv6, 443, true}) } diff --git a/stats.go b/stats.go index 2cc8dd77..42e66ec9 100644 --- a/stats.go +++ b/stats.go @@ -178,7 +178,7 @@ UpConn: if c.PID == uint(proc.Pid) { for _, v := range rt.Config.Resources { for _, r := range v { - if c.RemotePort == r.URL.Port { + if c.RemotePort == uint16(r.URL.Port) { for _, ip := range ips[r.URL.Host] { if ip.Equal(c.RemoteAddress) { d++ @@ -210,7 +210,7 @@ func (rt *Runtime) LookUpResourceIps() map[string][]net.IP { return ips } -//log proc samples infinite loop +// log proc samples infinite loop func (rt *Runtime) logRuntimeStats(proc *process.Process) { go func() { for { diff --git a/upstream.go b/upstream.go index 804ed8bf..05dda562 100644 --- a/upstream.go +++ b/upstream.go @@ -4,14 +4,14 @@ import ( "strconv" ) -//URL describes host mapping +// URL describes host mapping type URL struct { Scheme string Host string - Port uint16 + Port int } -//String representation of our URL struct +// String representation of our URL struct func (u URL) String() string { return u.Scheme + "://" + u.Host + ":" + strconv.Itoa(int(u.Port)) }