Remove panic() and handle it appropriately (#5807)

This is an effort to remove panic from the source. 
Add a new call called CriticialIf, that calls LogIf and exits. 
Replace panics with one of CriticalIf, FatalIf and a return of error.
This commit is contained in:
ebozduman
2018-04-19 17:24:43 -07:00
committed by kannappanr
parent 846f3e8f59
commit f16bfda2f2
21 changed files with 129 additions and 128 deletions

View File

@@ -19,7 +19,6 @@ package target
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"sync/atomic"
"time"
@@ -118,24 +117,28 @@ func (target *HTTPClientTarget) Close() error {
return nil
}
func mustGetNewUUID() string {
func getNewUUID() (string, error) {
uuid, err := uuid.New()
if err != nil {
panic(fmt.Sprintf("%s. Unable to generate random UUID", err))
return "", err
}
return uuid.String()
return uuid.String(), nil
}
// NewHTTPClientTarget - creates new HTTP client target.
func NewHTTPClientTarget(host xnet.Host, w http.ResponseWriter) *HTTPClientTarget {
func NewHTTPClientTarget(host xnet.Host, w http.ResponseWriter) (*HTTPClientTarget, error) {
uuid, err := getNewUUID()
if err != nil {
return nil, err
}
c := &HTTPClientTarget{
id: event.TargetID{"httpclient" + "+" + mustGetNewUUID() + "+" + host.Name, host.Port.String()},
id: event.TargetID{"httpclient" + "+" + uuid + "+" + host.Name, host.Port.String()},
w: w,
eventCh: make(chan []byte),
DoneCh: make(chan struct{}),
stopCh: make(chan struct{}),
}
c.start()
return c
return c, nil
}

View File

@@ -112,7 +112,10 @@ func NewNATSTarget(id string, args NATSArgs) (*NATSTarget, error) {
clientID := args.Streaming.ClientID
if clientID == "" {
clientID = mustGetNewUUID()
clientID, err = getNewUUID()
if err != nil {
return nil, err
}
}
connOpts := []stan.Option{stan.NatsURL(addressURL)}