Skip to content

Commit

Permalink
chore: Improve error message consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
rg0now committed Nov 28, 2024
1 parent 032619f commit 74d5480
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 70 deletions.
8 changes: 4 additions & 4 deletions cmd/turncat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ func getStunnerConf(uri string) (*stnrv1.StunnerConfig, error) {
case "k8s":
conf, err := getStunnerConfFromK8s(def)
if err != nil {
return nil, fmt.Errorf("Could not read running STUNner configuration from "+
return nil, fmt.Errorf("could not read running STUNner configuration from "+
"Kubernetes: %w", err)
}
return conf, nil
case "turn":
conf, err := getStunnerConfFromCLI(def)
if err != nil {
return nil, fmt.Errorf("Could not generate STUNner configuration from "+
return nil, fmt.Errorf("could not generate STUNner configuration from "+
"URI %q: %w", uri, err)
}
return conf, nil
Expand Down Expand Up @@ -237,11 +237,11 @@ func getStunnerConfFromCLI(def string) (*stnrv1.StunnerConfig, error) {

u, err := stunner.ParseUri(uri)
if err != nil {
return nil, fmt.Errorf("Invalid STUNner URI %q: %s", uri, err)
return nil, fmt.Errorf("invalid STUNner URI %q: %s", uri, err)
}

if u.Username == "" || u.Password == "" {
return nil, fmt.Errorf("Username/password must be set: '%s'", uri)
return nil, fmt.Errorf("username/password must be set: '%s'", uri)
}

conf.Listeners[0].PublicAddr = u.Address
Expand Down
2 changes: 1 addition & 1 deletion internal/object/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (a *Admin) reconcileMetrics(req *stnrv1.AdminConfig) error {
a.log.Tracef("closing metrics server at %s", mEndpoint)

if err := a.metricsServer.Shutdown(context.Background()); err != nil {
return fmt.Errorf("error stopping metrics server at %s: %w",
return fmt.Errorf("failed to stop metrics server at %s: %w",
mEndpoint, err)
}
a.metricsServer = nil
Expand Down
2 changes: 1 addition & 1 deletion internal/object/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import "errors"

var (
// ErrRestartRequired indicates that an object needs to be restarted for reconciliation.
ErrRestartRequired = errors.New("Restart required")
ErrRestartRequired = errors.New("restart required")
)
2 changes: 1 addition & 1 deletion internal/resolver/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ func (m *MockResolver) Lookup(domain string) ([]net.IP, error) {
}
}

return []net.IP{}, fmt.Errorf("Host %q not found: 3(NXDOMAIN)", domain)
return []net.IP{}, fmt.Errorf("host %q not found: 3(NXDOMAIN)", domain)
}
6 changes: 3 additions & 3 deletions internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ func doResolve(e *serviceEntry) error {
if e.cname == "" {
cname, err := e.resolver.LookupCNAME(e.ctx, e.domain)
if err != nil {
return fmt.Errorf("Cannot resolve CNAME for domain %q: %s",
return fmt.Errorf("failed to resolve CNAME for domain %q: %s",
e.domain, err.Error())
}
e.cname = cname
}

hosts, err := e.resolver.LookupHost(e.ctx, e.domain)
if err != nil {
return fmt.Errorf("Cannot resolve CNAME for domain %q: %s",
return fmt.Errorf("failed to resolve CNAME for domain %q: %s",
e.domain, err.Error())
}

Expand Down Expand Up @@ -175,7 +175,7 @@ func (r *dnsResolverImpl) Lookup(domain string) ([]net.IP, error) {

e, found := r.register[domain]
if !found {
return []net.IP{}, fmt.Errorf("Unknown domain name: %q", domain)
return []net.IP{}, fmt.Errorf("unknown domain name: %q", domain)
}

e.lock.RLock()
Expand Down
6 changes: 3 additions & 3 deletions internal/util/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ func ParseEndpoint(ep string) (*Endpoint, error) {
} else {
port, err = strconv.Atoi(m[2])
if err != nil {
return nil, fmt.Errorf("Invalid port in endpoint %q: %w", ep, err)
return nil, fmt.Errorf("invalid port in endpoint %q: %w", ep, err)
}
endPort, err = strconv.Atoi(m[3])
if err != nil {
return nil, fmt.Errorf("Invalid end-port in endpoint %q: %w", ep, err)
return nil, fmt.Errorf("invalid end-port in endpoint %q: %w", ep, err)
}
cidr = m[1]
hasPort = true
Expand All @@ -56,7 +56,7 @@ func ParseEndpoint(ep string) (*Endpoint, error) {
// convert to an actual prefix
_, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, fmt.Errorf("Invalid endpoint %q: %w", ep, err)
return nil, fmt.Errorf("invalid endpoint %q: %w", ep, err)
}

return &Endpoint{
Expand Down
3 changes: 1 addition & 2 deletions pkg/apis/v1/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ func (req *AuthConfig) Validate() error {
case AuthTypeEphemeral:
_, secretFound := req.Credentials["secret"]
if !secretFound {
return fmt.Errorf("cannot handle auth config for type %s: invalid secret",
atype.String())
return fmt.Errorf("no secret found in %s auth config", atype.String())
}
default:
return fmt.Errorf("invalid authentication type %q", req.Type)
Expand Down
8 changes: 4 additions & 4 deletions pkg/apis/v1/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
)

var (
ErrInvalidConf = errors.New("Invalid configuration")
ErrNoSuchListener = errors.New("No such listener")
ErrNoSuchCluster = errors.New("No such cluster")
// ErrInvalidRoute = errors.New("Invalid route")
ErrInvalidConf = errors.New("invalid configuration")
ErrNoSuchListener = errors.New("no such listener")
ErrNoSuchCluster = errors.New("no such cluster")
// ErrInvalidRoute = errors.New("invalid route")
)

type ErrRestarted struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/pion/logging"
)

var errFileTruncated = errors.New("Zero-length config file")
var errFileTruncated = errors.New("zero-length config file")

var (
// Send pings to the CDS server with this period. Must be less than PongWait.
Expand Down Expand Up @@ -49,7 +49,7 @@ type Client interface {
func New(origin string, id string, logger logging.LoggerFactory) (Client, error) {
u, err := getURI(origin)
if err != nil {
return nil, fmt.Errorf("could not parse config origin URI %q: %w", origin, err)
return nil, fmt.Errorf("failed to parse config origin URI %q: %w", origin, err)
}

switch strings.ToLower(u.Scheme) {
Expand Down
12 changes: 6 additions & 6 deletions pkg/config/client/file_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (w *ConfigFileClient) String() string {
func (w *ConfigFileClient) Load() (*stnrv1.StunnerConfig, error) {
b, err := os.ReadFile(w.configFile)
if err != nil {
return nil, fmt.Errorf("could not read config file %q: %s", w.configFile, err.Error())
return nil, fmt.Errorf("failed to read config file %q: %s", w.configFile, err.Error())
}

if len(b) == 0 {
Expand All @@ -54,7 +54,7 @@ func (w *ConfigFileClient) Load() (*stnrv1.StunnerConfig, error) {

c, err := ParseConfig(b)
if err != nil {
return nil, fmt.Errorf("could not parse config: %w", err)
return nil, fmt.Errorf("failed to parse config: %w", err)
}

if err := c.Validate(); err != nil {
Expand Down Expand Up @@ -128,7 +128,7 @@ func (w *ConfigFileClient) Poll(ctx context.Context, ch chan<- *stnrv1.StunnerCo

case e, ok := <-watcher.Events:
if !ok {
return errors.New("Config watcher received invalid event")
return errors.New("config watcher received invalid event")
}

w.log.Debugf("Received watch event: %s", e.String())
Expand All @@ -139,7 +139,7 @@ func (w *ConfigFileClient) Poll(ctx context.Context, ch chan<- *stnrv1.StunnerCo
config, err.Error())
}

return fmt.Errorf("Config file deleted %q, disabling watcher", e.Op.String())
return fmt.Errorf("config file deleted %q, disabling watcher", e.Op.String())
}

if !e.Has(fsnotify.Write) {
Expand Down Expand Up @@ -178,15 +178,15 @@ func (w *ConfigFileClient) Poll(ctx context.Context, ch chan<- *stnrv1.StunnerCo

case err, ok := <-watcher.Errors:
if !ok {
return errors.New("Config watcher error handler received invalid error")
return errors.New("config watcher error handler received invalid error")
}

if err := watcher.Remove(config); err != nil {
w.log.Debugf("Could not remove config file %q watcher: %s",
config, err.Error())
}

return fmt.Errorf("Config watcher error, deactivating: %w", err)
return fmt.Errorf("config watcher error, deactivating: %w", err)
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/config/client/k8s_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,23 +237,23 @@ func DiscoverK8sStunnerdPods(ctx context.Context, k8sFlags *cliopt.ConfigFlags,
appLabel, err := labels.NewRequirement(stnrv1.DefaultAppLabelKey,
selection.Equals, []string{stnrv1.DefaultAppLabelValue})
if err != nil {
return ps, fmt.Errorf("cannot create app label selector: %w", err)
return ps, fmt.Errorf("failed to create app label selector: %w", err)
}
selector = selector.Add(*appLabel)

if gwNs != "" {
nsLabel, err := labels.NewRequirement(stnrv1.DefaultRelatedGatewayNamespace,
selection.Equals, []string{gwNs})
if err != nil {
return ps, fmt.Errorf("cannot create namespace label selector: %w", err)
return ps, fmt.Errorf("failed to create namespace label selector: %w", err)
}
selector = selector.Add(*nsLabel)

if gw != "" {
gwLabel, err := labels.NewRequirement(stnrv1.DefaultRelatedGatewayKey,
selection.Equals, []string{gw})
if err != nil {
return ps, fmt.Errorf("cannot create namespace label selector: %w", err)
return ps, fmt.Errorf("failed to create namespace label selector: %w", err)
}
selector = selector.Add(*gwLabel)
}
Expand Down Expand Up @@ -344,7 +344,7 @@ func DiscoverK8sAuthServer(ctx context.Context, k8sFlags *cliopt.ConfigFlags, au
LabelSelector: label,
})
if err != nil {
return PodInfo{}, fmt.Errorf("Failed to query Kubernetes API server: %w", err)
return PodInfo{}, fmt.Errorf("failed to query Kubernetes API server: %w", err)
}

if len(pods.Items) == 0 {
Expand Down Expand Up @@ -376,7 +376,7 @@ func (d *PodConnector) portfwd(ctx context.Context, pod *corev1.Pod, port int) (
d.log.Debugf("Creating a SPDY stream to API server using URL %q", req.URL().String())
transport, upgrader, err := spdy.RoundTripperFor(d.config)
if err != nil {
return PodInfo{}, fmt.Errorf("error getting transport/upgrader from restconfig: %w", err)
return PodInfo{}, fmt.Errorf("failed to get transport/upgrader from restconfig: %w", err)
}

dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, http.MethodPost, req.URL())
Expand All @@ -387,12 +387,12 @@ func (d *PodConnector) portfwd(ctx context.Context, pod *corev1.Pod, port int) (
out, errOut := new(bytes.Buffer), new(bytes.Buffer)
fw, err := portforward.New(dialer, []string{remoteAddr}, stopChan, readyChan, out, errOut)
if err != nil {
return PodInfo{}, fmt.Errorf("Error creating port-forwarder: %w", err)
return PodInfo{}, fmt.Errorf("failed to create port-forwarder: %w", err)
}

go func() {
if err := fw.ForwardPorts(); err != nil {
d.log.Errorf("Error setting up port-forwarder: %s", err.Error())
d.log.Errorf("failed to set up port-forwarder: %s", err.Error())
os.Exit(1)
}
}()
Expand Down
26 changes: 13 additions & 13 deletions pkg/whipconn/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ func (d *Dialer) DialContext(ctx context.Context, addr string) (net.Conn, error)
ICETransportPolicy: d.config.ICETransportPolicy,
})
if err != nil {
return nil, fmt.Errorf("Failed to create PeerConnection: %w", err)
return nil, fmt.Errorf("failed to create PeerConnection: %w", err)
}

d.log.Trace("Creating DataChannel")
dataChannel, err := peerConn.CreateDataChannel("whipconn", nil)
if err != nil {
return nil, fmt.Errorf("Failed to create DataChannel: %w", err)
return nil, fmt.Errorf("failed to create DataChannel: %w", err)
}

conn := &dialerConn{
Expand All @@ -77,7 +77,7 @@ func (d *Dialer) DialContext(ctx context.Context, addr string) (net.Conn, error)

raw, err := dataChannel.Detach()
if err != nil {
errCh <- fmt.Errorf("Failed to detach DataChannel: %w", err)
errCh <- fmt.Errorf("failed to detach DataChannel: %w", err)
}
conn.dataConn = raw

Expand All @@ -95,13 +95,13 @@ func (d *Dialer) DialContext(ctx context.Context, addr string) (net.Conn, error)
offer, err := peerConn.CreateOffer(nil)
if err != nil {
conn.Close() //nolint
return nil, fmt.Errorf("Failed to create offer: %w", err)
return nil, fmt.Errorf("failed to create offer: %w", err)
}

err = peerConn.SetLocalDescription(offer)
if err != nil {
conn.Close() //nolint
return nil, fmt.Errorf("Failed to set local SDP (Offer): %w", err)
return nil, fmt.Errorf("failed to set local SDP (Offer): %w", err)
}

// Block until ICE Gathering is complete, disabling trickle ICE
Expand All @@ -118,7 +118,7 @@ func (d *Dialer) DialContext(ctx context.Context, addr string) (net.Conn, error)
makeURL(addr, d.config.Endpoint).String(), bytes.NewBuffer(sdp))
if err != nil {
conn.Close() //nolint
return nil, fmt.Errorf("Unexpected error building HTTP request: %w", err)
return nil, fmt.Errorf("unexpected error building HTTP request: %w", err)
}

req.Header.Add("Content-Type", "application/sdp")
Expand All @@ -129,7 +129,7 @@ func (d *Dialer) DialContext(ctx context.Context, addr string) (net.Conn, error)
resp, err := http.DefaultClient.Do(req)
if err != nil {
conn.Close() //nolint
return nil, fmt.Errorf("Failed to POST WHIP request: %w", err)
return nil, fmt.Errorf("failed to POST WHIP request: %w", err)
}

d.log.Tracef("Received POST response with status code: %d", resp.StatusCode)
Expand All @@ -142,14 +142,14 @@ func (d *Dialer) DialContext(ctx context.Context, addr string) (net.Conn, error)
body, err := io.ReadAll(resp.Body)
if err != nil {
conn.Close() //nolint
return nil, fmt.Errorf("Failed to read HTTP response body: %w", err)
return nil, fmt.Errorf("failed to read HTTP response body: %w", err)
}
defer resp.Body.Close()

resourceId := resp.Header.Get("Location")
if resourceId == "" {
conn.Close() //nolint
return nil, errors.New("Empty resource id in POST response")
return nil, errors.New("empty resource id in POST response")
}
conn.resourceId = resourceId

Expand All @@ -160,7 +160,7 @@ func (d *Dialer) DialContext(ctx context.Context, addr string) (net.Conn, error)
err = peerConn.SetRemoteDescription(answer)
if err != nil {
conn.Close() //nolint
return nil, fmt.Errorf("Failed to set remote SDP (Answer): %w", err)
return nil, fmt.Errorf("failed to set remote SDP (Answer): %w", err)
}

// Waiting for the connection or errors surfaced from the callbacks
Expand Down Expand Up @@ -198,19 +198,19 @@ func (c *dialerConn) Close() error {
uri := makeURL(c.addr, makeResourceURL(c.dialer.config.Endpoint, c.resourceId))
req, err := http.NewRequest("DELETE", uri.String(), nil)
if err != nil {
return fmt.Errorf("Unexpected error building http request: %w", err)
return fmt.Errorf("unexpected error building http request: %w", err)
}
if c.dialer.config.Token != "" {
req.Header.Add("Authorization", "Bearer "+c.dialer.config.Token)
}

if _, err = http.DefaultClient.Do(req); err != nil {
return fmt.Errorf("Failed WHIP DELETE request: %w", err)
return fmt.Errorf("failed WHIP DELETE request: %w", err)
}

// Close the peerconnection
if err := c.peerConn.Close(); err != nil {
return fmt.Errorf("Failed to close PeerConnection: %w", err)
return fmt.Errorf("failed to close PeerConnection: %w", err)
}

return nil
Expand Down
Loading

0 comments on commit 74d5480

Please sign in to comment.