Skip to content

Commit

Permalink
better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmittag committed Jun 11, 2023
1 parent 62493e9 commit 6701162
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
12 changes: 6 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,27 +165,27 @@ 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")
} else {
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))
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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})
}
Expand All @@ -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})
}
Expand Down
4 changes: 2 additions & 2 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 6701162

Please sign in to comment.