Skip to content

Commit

Permalink
feat: enhance error handling in probe initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
macrat committed Apr 11, 2021
1 parent 70694bd commit a150835
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 42 deletions.
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ func ParseArgs(args []string) ([]Task, []error) {
p, err := probe.Get(a)
if err != nil {
switch err {
case probe.UnsupportedSchemeError:
case probe.ErrUnsupportedScheme:
err = fmt.Errorf("%s: This scheme is not supported.", a)
case probe.MissingSchemeError:
err = fmt.Errorf("%s: Not valid as schedule or target URI. Please specify scheme if this is target. (e.g. ping:%s or http://%s)", a, a, a)
default:
case probe.ErrMissingScheme:
err = fmt.Errorf("%s: Not valid as schedule or target URI. Please specify scheme if this is target. (e.g. ping:example.local or http://example.com)", a)
case probe.ErrInvalidURI:
err = fmt.Errorf("%s: Not valid as schedule or target URI.", a)
default:
err = fmt.Errorf("%s: %s", a, err)
}
errors = append(errors, err)
continue
Expand Down
6 changes: 3 additions & 3 deletions probe/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ type DNSProbe struct {
target *url.URL
}

func NewDNSProbe(u *url.URL) DNSProbe {
func NewDNSProbe(u *url.URL) (DNSProbe, error) {
if u.Opaque != "" {
return DNSProbe{&url.URL{Scheme: "dns", Opaque: u.Opaque}}
return DNSProbe{&url.URL{Scheme: "dns", Opaque: u.Opaque}}, nil
} else {
return DNSProbe{&url.URL{Scheme: "dns", Opaque: u.Hostname()}}
return DNSProbe{&url.URL{Scheme: "dns", Opaque: u.Hostname()}}, nil
}
}

Expand Down
4 changes: 2 additions & 2 deletions probe/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type ExecuteProbe struct {
env []string
}

func NewExecuteProbe(u *url.URL) ExecuteProbe {
func NewExecuteProbe(u *url.URL) (ExecuteProbe, error) {
p := ExecuteProbe{}

path := u.Opaque
Expand All @@ -36,7 +36,7 @@ func NewExecuteProbe(u *url.URL) ExecuteProbe {
p.env = append(p.env, fmt.Sprintf("%s=%s", k, v[len(v)-1]))
}

return p
return p, nil
}

func (p ExecuteProbe) Target() *url.URL {
Expand Down
17 changes: 11 additions & 6 deletions probe/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package probe

import (
"errors"
"fmt"
"net"
"net/http"
"net/url"
Expand All @@ -17,7 +18,7 @@ const (
)

var (
RedirectLoopDetected = errors.New("redirect loop detected")
ErrRedirectLoopDetected = errors.New("redirect loop detected")
)

type HTTPProbe struct {
Expand All @@ -27,19 +28,23 @@ type HTTPProbe struct {
client *http.Client
}

func NewHTTPProbe(u *url.URL) HTTPProbe {
func NewHTTPProbe(u *url.URL) (HTTPProbe, error) {
ucopy := *u
requrl := &ucopy

scheme := strings.Split(requrl.Scheme, "-")
requrl.Scheme = scheme[0]

method := "GET"
var method string
if len(scheme) > 1 {
m := strings.ToUpper(scheme[1])
switch m {
case "HEAD", "POST", "OPTIONS":
case "":
method = "GET"
case "GET", "HEAD", "POST", "OPTIONS":
method = m
default:
return HTTPProbe{}, fmt.Errorf("HTTP \"%s\" method is not supported. Please use GET, HEAD, POST, or OPTIONS.", m)
}
}

Expand All @@ -54,12 +59,12 @@ func NewHTTPProbe(u *url.URL) HTTPProbe {
},
CheckRedirect: func(req *http.Request, via []*http.Request) error {
if len(via) > HTTP_REDIRECT_MAX {
return RedirectLoopDetected
return ErrRedirectLoopDetected
}
return nil
},
},
}
}, nil
}

func (p HTTPProbe) Target() *url.URL {
Expand Down
6 changes: 3 additions & 3 deletions probe/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ type PingProbe struct {
target *url.URL
}

func NewPingProbe(u *url.URL) PingProbe {
func NewPingProbe(u *url.URL) (PingProbe, error) {
if u.Opaque != "" {
return PingProbe{&url.URL{Scheme: "ping", Opaque: u.Opaque}}
return PingProbe{&url.URL{Scheme: "ping", Opaque: u.Opaque}}, nil
} else {
return PingProbe{&url.URL{Scheme: "ping", Opaque: u.Hostname()}}
return PingProbe{&url.URL{Scheme: "ping", Opaque: u.Hostname()}}, nil
}
}

Expand Down
33 changes: 14 additions & 19 deletions probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ package probe
import (
"errors"
"net/url"
"strings"

"github.com/macrat/ayd/store"
)

var (
InvalidURIError = errors.New("invalid URI")
MissingSchemeError = errors.New("missing scheme")
UnsupportedSchemeError = errors.New("unsupported scheme")
ErrInvalidURI = errors.New("invalid URI")
ErrMissingScheme = errors.New("missing scheme in URI")
ErrUnsupportedScheme = errors.New("unsupported scheme")
)

type Probe interface {
Target() *url.URL
Check() store.Record
}

func GetByURL(u *url.URL) Probe {
func GetByURL(u *url.URL) (Probe, error) {
if strings.HasPrefix(u.Scheme, "http-") || strings.HasPrefix(u.Scheme, "https-") {
return NewHTTPProbe(u)
}

switch u.Scheme {
case
"http", "https",
"http-get", "https-get",
"http-head", "https-head",
"http-post", "https-post",
"http-options", "https-options":
case "http", "https":
return NewHTTPProbe(u)
case "ping":
return NewPingProbe(u)
Expand All @@ -36,24 +36,19 @@ func GetByURL(u *url.URL) Probe {
case "exec":
return NewExecuteProbe(u)
default:
return nil
return nil, ErrUnsupportedScheme
}
}

func Get(rawURL string) (Probe, error) {
u, err := url.Parse(rawURL)
if err != nil {
return nil, InvalidURIError
return nil, ErrInvalidURI
}

if u.Scheme == "" {
return nil, MissingSchemeError
}

p := GetByURL(u)
if p == nil {
return nil, UnsupportedSchemeError
return nil, ErrMissingScheme
}

return p, nil
return GetByURL(u)
}
13 changes: 8 additions & 5 deletions probe/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ type TCPProbe struct {
target *url.URL
}

func NewTCPProbe(u *url.URL) TCPProbe {
if u.Opaque != "" {
return TCPProbe{&url.URL{Scheme: "tcp", Opaque: u.Opaque}}
} else {
return TCPProbe{&url.URL{Scheme: "tcp", Opaque: u.Host}}
func NewTCPProbe(u *url.URL) (TCPProbe, error) {
p := TCPProbe{&url.URL{Scheme: "tcp", Opaque: u.Opaque}}
if u.Opaque == "" {
p.target.Opaque = u.Host
}
if _, _, err := net.SplitHostPort(p.target.Opaque); err != nil {
return TCPProbe{}, err
}
return p, nil
}

func (p TCPProbe) Target() *url.URL {
Expand Down

0 comments on commit a150835

Please sign in to comment.