panic wrapper functions

This commit is contained in:
Ethan Buchman
2015-07-19 23:42:52 +00:00
parent 5983088a32
commit 8e50bf15de
45 changed files with 229 additions and 275 deletions

View File

@@ -200,7 +200,7 @@ func (a *AddrBook) PickAddress(newBias int) *NetAddress {
}
randIndex--
}
panic("Should not happen")
PanicSanity("Should not happen")
} else {
// pick random New bucket.
var bucket map[string]*knownAddress = nil
@@ -215,7 +215,7 @@ func (a *AddrBook) PickAddress(newBias int) *NetAddress {
}
randIndex--
}
panic("Should not happen")
PanicSanity("Should not happen")
}
return nil
}
@@ -332,14 +332,14 @@ func (a *AddrBook) loadFromFile(filePath string) bool {
// Load addrBookJSON{}
r, err := os.Open(filePath)
if err != nil {
panic(Fmt("Error opening file %s: %v", filePath, err))
PanicCrisis(Fmt("Error opening file %s: %v", filePath, err))
}
defer r.Close()
aJSON := &addrBookJSON{}
dec := json.NewDecoder(r)
err = dec.Decode(aJSON)
if err != nil {
panic(Fmt("Error reading file %s: %v", filePath, err))
PanicCrisis(Fmt("Error reading file %s: %v", filePath, err))
}
// Restore all the fields...
@@ -388,7 +388,8 @@ func (a *AddrBook) getBucket(bucketType byte, bucketIdx int) map[string]*knownAd
case bucketTypeOld:
return a.addrOld[bucketIdx]
default:
panic("Should not happen")
PanicSanity("Should not happen")
return nil
}
}

View File

@@ -420,7 +420,7 @@ FOR_LOOP:
}
channel, ok := c.channelsIdx[pkt.ChannelId]
if !ok || channel == nil {
panic(Fmt("Unknown channel %X", pkt.ChannelId))
PanicQ(Fmt("Unknown channel %X", pkt.ChannelId))
}
msgBytes, err := channel.recvMsgPacket(pkt)
if err != nil {
@@ -435,7 +435,7 @@ FOR_LOOP:
c.onReceive(pkt.ChannelId, msgBytes)
}
default:
panic(Fmt("Unknown message type %X", pktType))
PanicSanity(Fmt("Unknown message type %X", pktType))
}
// TODO: shouldn't this go in the sendRoutine?
@@ -485,7 +485,7 @@ type Channel struct {
func newChannel(conn *MConnection, desc *ChannelDescriptor) *Channel {
desc.FillDefaults()
if desc.Priority <= 0 {
panic("Channel default priority must be a postive integer")
PanicSanity("Channel default priority must be a postive integer")
}
return &Channel{
conn: conn,

View File

@@ -35,11 +35,11 @@ const (
func splitHostPort(addr string) (host string, port int) {
host, portStr, err := net.SplitHostPort(addr)
if err != nil {
panic(err)
PanicSanity(err)
}
port, err = strconv.Atoi(portStr)
if err != nil {
panic(err)
PanicSanity(err)
}
return host, port
}
@@ -51,7 +51,7 @@ func NewDefaultListener(protocol string, lAddr string, requireUPNPHairpin bool)
// Create listener
listener, err := net.Listen(protocol, lAddr)
if err != nil {
panic(err)
PanicCrisis(err)
}
// Actual listener local IP & port
listenerIP, listenerPort := splitHostPort(listener.Addr().String())
@@ -83,7 +83,7 @@ SKIP_UPNP:
extAddr = getNaiveExternalAddress(listenerPort)
}
if extAddr == nil {
panic("Could not determine external address!")
PanicCrisis("Could not determine external address!")
}
dl := &DefaultListener{
@@ -117,7 +117,7 @@ func (l *DefaultListener) listenRoutine() {
// listener wasn't stopped,
// yet we encountered an error.
if err != nil {
panic(err)
PanicCrisis(err)
}
l.connections <- conn
@@ -190,7 +190,7 @@ func getUPNPExternalAddress(externalPort, internalPort int) *NetAddress {
func getNaiveExternalAddress(port int) *NetAddress {
addrs, err := net.InterfaceAddrs()
if err != nil {
panic(Fmt("Could not fetch interface addresses: %v", err))
PanicCrisis(Fmt("Could not fetch interface addresses: %v", err))
}
for _, a := range addrs {

View File

@@ -9,6 +9,8 @@ import (
"net"
"strconv"
"time"
. "github.com/tendermint/tendermint/common"
)
type NetAddress struct {
@@ -21,7 +23,7 @@ type NetAddress struct {
func NewNetAddress(addr net.Addr) *NetAddress {
tcpAddr, ok := addr.(*net.TCPAddr)
if !ok {
panic(fmt.Sprintf("Only TCPAddrs are supported. Got: %v", addr))
PanicSanity(fmt.Sprintf("Only TCPAddrs are supported. Got: %v", addr))
}
ip := tcpAddr.IP
port := uint16(tcpAddr.Port)
@@ -32,21 +34,21 @@ func NewNetAddress(addr net.Addr) *NetAddress {
func NewNetAddressString(addr string) *NetAddress {
host, portStr, err := net.SplitHostPort(addr)
if err != nil {
panic(err)
PanicSanity(err)
}
ip := net.ParseIP(host)
if ip == nil {
if len(host) > 0 {
ips, err := net.LookupIP(host)
if err != nil {
panic(err)
PanicSanity(err)
}
ip = ips[0]
}
}
port, err := strconv.ParseUint(portStr, 10, 16)
if err != nil {
panic(err)
PanicSanity(err)
}
na := NewNetAddressIPPort(ip, uint16(port))
return na
@@ -76,7 +78,8 @@ func (na *NetAddress) Less(other interface{}) bool {
if o, ok := other.(*NetAddress); ok {
return na.String() < o.String()
} else {
panic("Cannot compare unequal types")
PanicSanity("Cannot compare unequal types")
return false
}
}

View File

@@ -52,7 +52,7 @@ func newPeer(conn net.Conn, peerNodeInfo *types.NodeInfo, outbound bool, reactor
onReceive := func(chId byte, msgBytes []byte) {
reactor := reactorsByCh[chId]
if reactor == nil {
panic(Fmt("Unknown channel %X", chId))
PanicSanity(Fmt("Unknown channel %X", chId))
}
reactor.Receive(chId, p, msgBytes)
}

View File

@@ -190,7 +190,7 @@ func genEphKeys() (ephPub, ephPriv *[32]byte) {
var err error
ephPub, ephPriv, err = box.GenerateKey(crand.Reader)
if err != nil {
panic("Could not generate ephemeral keypairs")
PanicCrisis("Could not generate ephemeral keypairs")
}
return
}

View File

@@ -98,7 +98,7 @@ func (sw *Switch) AddReactor(name string, reactor Reactor) Reactor {
for _, chDesc := range reactorChannels {
chId := chDesc.Id
if sw.reactorsByCh[chId] != nil {
panic(fmt.Sprintf("Channel %X has multiple reactors %v & %v", chId, sw.reactorsByCh[chId], reactor))
PanicSanity(fmt.Sprintf("Channel %X has multiple reactors %v & %v", chId, sw.reactorsByCh[chId], reactor))
}
sw.chDescs = append(sw.chDescs, chDesc)
sw.reactorsByCh[chId] = reactor