Skip to content
Snippets Groups Projects
Commit 017846ea authored by Julian Kornberger's avatar Julian Kornberger Committed by takt
Browse files

Wrap errors (#161)

Package errors provides simple error handling primitives.
Read more on https://github.com/pkg/errors
parent 857732ea
No related branches found
No related tags found
No related merge requests found
Showing
with 61 additions and 56 deletions
......@@ -2,7 +2,8 @@ package packet
import (
"bytes"
"fmt"
"github.com/pkg/errors"
)
// TerminationMessage represents a termination message
......@@ -28,7 +29,7 @@ func decodeTerminationMessage(buf *bytes.Buffer, ch *CommonHeader) (*Termination
for read < toRead {
tlv, err := decodeInformationTLV(buf)
if err != nil {
return nil, fmt.Errorf("Unable to decode TLV: %v", err)
return nil, errors.Wrap(err, "Unable to decode TLV")
}
tm.TLVs = append(tm.TLVs, tlv)
......
......@@ -9,6 +9,7 @@ import (
"github.com/bio-routing/bio-rd/protocols/bgp/packet"
"github.com/bio-routing/bio-rd/routingtable/locRIB"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/taktv6/tflow2/convert"
)
......@@ -70,7 +71,7 @@ func recvMsg(c net.Conn) (msg []byte, err error) {
buffer := make([]byte, defaultBufferLen)
_, err = io.ReadFull(c, buffer[0:packet.MinLen])
if err != nil {
return nil, fmt.Errorf("Read failed: %v", err)
return nil, errors.Wrap(err, "Read failed")
}
l := convert.Uint32b(buffer[1:3])
......@@ -83,7 +84,7 @@ func recvMsg(c net.Conn) (msg []byte, err error) {
toRead := l
_, err = io.ReadFull(c, buffer[packet.MinLen:toRead])
if err != nil {
return nil, fmt.Errorf("Read failed: %v", err)
return nil, errors.Wrap(err, "Read failed")
}
return buffer, nil
......
package device
import (
"fmt"
"sync"
"github.com/pkg/errors"
)
// Server represents a device server
......@@ -29,7 +30,7 @@ func New() (*Server, error) {
srv := newWithAdapter(nil)
err := srv.loadAdapter()
if err != nil {
return nil, fmt.Errorf("Unable to create OS adapter: %v", err)
return nil, errors.Wrap(err, "Unable to create OS adapter")
}
return srv, nil
......@@ -48,7 +49,7 @@ func newWithAdapter(a osAdapter) *Server {
func (ds *Server) Start() error {
err := ds.osAdapter.start()
if err != nil {
return fmt.Errorf("Unable to start osAdapter: %v", err)
return errors.Wrap(err, "Unable to start osAdapter")
}
return nil
......
package device
import (
"fmt"
bnet "github.com/bio-routing/bio-rd/net"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
)
......@@ -11,7 +10,7 @@ import (
func (ds *Server) loadAdapter() error {
a, err := newOSAdapterLinux(ds)
if err != nil {
return fmt.Errorf("Unable to create linux adapter: %v", err)
return errors.Wrap(err, "Unable to create linux adapter")
}
ds.osAdapter = a
......@@ -31,7 +30,7 @@ func newOSAdapterLinux(srv *Server) (*osAdapterLinux, error) {
h, err := netlink.NewHandle()
if err != nil {
return nil, fmt.Errorf("Failed to create netlink handle: %v", err)
return nil, errors.Wrap(err, "Failed to create netlink handle")
}
o.handle = h
......@@ -42,18 +41,18 @@ func (o *osAdapterLinux) start() error {
chLU := make(chan netlink.LinkUpdate)
err := netlink.LinkSubscribe(chLU, o.done)
if err != nil {
return fmt.Errorf("Unable to subscribe for link updates: %v", err)
return errors.Wrap(err, "Unable to subscribe for link updates")
}
chAU := make(chan netlink.AddrUpdate)
err = netlink.AddrSubscribe(chAU, o.done)
if err != nil {
return fmt.Errorf("Unable to subscribe for address updates: %v", err)
return errors.Wrap(err, "Unable to subscribe for address updates")
}
err = o.init()
if err != nil {
return fmt.Errorf("Init failed: %v", err)
return errors.Wrap(err, "Init failed")
}
go o.monitorLinks(chLU)
......@@ -65,7 +64,7 @@ func (o *osAdapterLinux) start() error {
func (o *osAdapterLinux) init() error {
links, err := o.handle.LinkList()
if err != nil {
return fmt.Errorf("Unable to get links: %v", err)
return errors.Wrap(err, "Unable to get links")
}
for _, l := range links {
......@@ -74,7 +73,7 @@ func (o *osAdapterLinux) init() error {
for _, f := range []int{4, 6} {
addrs, err := o.handle.AddrList(l, f)
if err != nil {
return fmt.Errorf("Unable to get addresses for interface %s: %v", d.Name, err)
return errors.Wrapf(err, "Unable to get addresses for interface %s", d.Name)
}
for _, addr := range addrs {
......
......@@ -2,13 +2,13 @@ package packet
import (
"bytes"
"fmt"
"math"
"sort"
"github.com/bio-routing/bio-rd/protocols/isis/types"
"github.com/bio-routing/bio-rd/util/decode"
umath "github.com/bio-routing/bio-rd/util/math"
"github.com/pkg/errors"
"github.com/taktv6/tflow2/convert"
)
......@@ -133,7 +133,7 @@ func DecodeCSNP(buf *bytes.Buffer) (*CSNP, error) {
err := decode.Decode(buf, fields)
if err != nil {
return nil, fmt.Errorf("Unable to decode fields: %v", err)
return nil, errors.Wrap(err, "Unable to decode fields")
}
nEntries := (csnp.PDULength - CSNPMinLen) / LSPEntryLen
......@@ -141,7 +141,7 @@ func DecodeCSNP(buf *bytes.Buffer) (*CSNP, error) {
for i := uint16(0); i < nEntries; i++ {
lspEntry, err := decodeLSPEntry(buf)
if err != nil {
return nil, fmt.Errorf("Unable to get LSPEntries: %v", err)
return nil, errors.Wrap(err, "Unable to get LSPEntries")
}
csnp.LSPEntries[i] = *lspEntry
}
......
......@@ -2,9 +2,9 @@ package packet
import (
"bytes"
"fmt"
"github.com/bio-routing/bio-rd/util/decode"
"github.com/pkg/errors"
)
// ISISHeader represents an ISIS header
......@@ -42,7 +42,7 @@ func DecodeHeader(buf *bytes.Buffer) (*ISISHeader, error) {
err := decode.Decode(buf, fields)
if err != nil {
return nil, fmt.Errorf("Unable to decode fields: %v", err)
return nil, errors.Wrap(err, "Unable to decode fields")
}
return h, nil
......
......@@ -2,10 +2,10 @@ package packet
import (
"bytes"
"fmt"
"github.com/bio-routing/bio-rd/protocols/isis/types"
"github.com/bio-routing/bio-rd/util/decode"
"github.com/pkg/errors"
"github.com/taktv6/tflow2/convert"
)
......@@ -63,12 +63,12 @@ func DecodeP2PHello(buf *bytes.Buffer) (*P2PHello, error) {
err := decode.Decode(buf, fields)
if err != nil {
return nil, fmt.Errorf("Unable to decode fields: %v", err)
return nil, errors.Wrap(err, "Unable to decode fields")
}
TLVs, err := readTLVs(buf)
if err != nil {
return nil, fmt.Errorf("Unable to read TLVs: %v", err)
return nil, errors.Wrap(err, "Unable to read TLVs")
}
pdu.TLVs = TLVs
......@@ -91,12 +91,12 @@ func DecodeL2Hello(buf *bytes.Buffer) (*L2Hello, error) {
err := decode.Decode(buf, fields)
if err != nil {
return nil, fmt.Errorf("Unable to decode fields: %v", err)
return nil, errors.Wrap(err, "Unable to decode fields")
}
TLVs, err := readTLVs(buf)
if err != nil {
return nil, fmt.Errorf("Unable to read TLVs: %v", err)
return nil, errors.Wrap(err, "Unable to read TLVs")
}
pdu.TLVs = TLVs
......
......@@ -2,7 +2,8 @@ package packet
import (
"bytes"
"fmt"
"github.com/pkg/errors"
)
const (
......@@ -33,7 +34,7 @@ func Decode(buf *bytes.Buffer) (*ISISPacket, error) {
hdr, err := DecodeHeader(buf)
if err != nil {
return nil, fmt.Errorf("Unable to decode header: %v", err)
return nil, errors.Wrap(err, "Unable to decode header")
}
pkt.Header = hdr
......@@ -41,7 +42,7 @@ func Decode(buf *bytes.Buffer) (*ISISPacket, error) {
case P2P_HELLO:
p2pHello, err := DecodeP2PHello(buf)
if err != nil {
return nil, fmt.Errorf("Unable to decode P2P hello: %v", err)
return nil, errors.Wrap(err, "Unable to decode P2P hello")
}
pkt.Body = p2pHello
}
......
......@@ -2,11 +2,11 @@ package packet
import (
"bytes"
"fmt"
"github.com/FMNSSun/libhash/fletcher"
"github.com/bio-routing/bio-rd/protocols/isis/types"
"github.com/bio-routing/bio-rd/util/decode"
"github.com/pkg/errors"
"github.com/taktv6/tflow2/convert"
)
......@@ -80,12 +80,12 @@ func DecodeLSPDU(buf *bytes.Buffer) (*LSPDU, error) {
err := decode.Decode(buf, fields)
if err != nil {
return nil, fmt.Errorf("Unable to decode fields: %v", err)
return nil, errors.Wrap(err, "Unable to decode fields")
}
TLVs, err := readTLVs(buf)
if err != nil {
return nil, fmt.Errorf("Unable to read TLVs: %v", err)
return nil, errors.Wrap(err, "Unable to read TLVs")
}
pdu.TLVs = TLVs
......
......@@ -2,9 +2,9 @@ package packet
import (
"bytes"
"fmt"
"github.com/bio-routing/bio-rd/util/decode"
"github.com/pkg/errors"
"github.com/taktv6/tflow2/convert"
)
......@@ -42,7 +42,7 @@ func decodeLSPEntry(buf *bytes.Buffer) (*LSPEntry, error) {
err := decode.Decode(buf, fields)
if err != nil {
return nil, fmt.Errorf("Unable to decode fields: %v", err)
return nil, errors.Wrap(err, "Unable to decode fields")
}
return lspEntry, nil
......
......@@ -2,12 +2,12 @@ package packet
import (
"bytes"
"fmt"
"math"
"github.com/bio-routing/bio-rd/protocols/isis/types"
"github.com/bio-routing/bio-rd/util/decode"
umath "github.com/bio-routing/bio-rd/util/math"
"github.com/pkg/errors"
"github.com/taktv6/tflow2/convert"
)
......@@ -81,7 +81,7 @@ func DecodePSNP(buf *bytes.Buffer) (*PSNP, error) {
err := decode.Decode(buf, fields)
if err != nil {
return nil, fmt.Errorf("Unable to decode fields: %v", err)
return nil, errors.Wrap(err, "Unable to decode fields")
}
nEntries := (psnp.PDULength - PSNPMinLen) / LSPEntryLen
......@@ -89,7 +89,7 @@ func DecodePSNP(buf *bytes.Buffer) (*PSNP, error) {
for i := uint16(0); i < nEntries; i++ {
lspEntry, err := decodeLSPEntry(buf)
if err != nil {
return nil, fmt.Errorf("Unable to get LSPEntries: %v", err)
return nil, errors.Wrap(err, "Unable to get LSPEntries")
}
psnp.LSPEntries[i] = *lspEntry
}
......
......@@ -2,9 +2,9 @@ package packet
import (
"bytes"
"fmt"
"github.com/bio-routing/bio-rd/util/decode"
"github.com/pkg/errors"
)
// TLV is an interface that all TLVs must fulfill
......@@ -42,7 +42,7 @@ func readTLVs(buf *bytes.Buffer) ([]TLV, error) {
for read < uint16(length) {
err = decode.Decode(buf, headFields)
if err != nil {
return nil, fmt.Errorf("Unable to decode fields: %v", err)
return nil, errors.Wrap(err, "Unable to decode fields")
}
read += 2
......@@ -69,7 +69,7 @@ func readTLVs(buf *bytes.Buffer) ([]TLV, error) {
}
if err != nil {
return nil, fmt.Errorf("Unable to read TLV: %v", err)
return nil, errors.Wrap(err, "Unable to read TLV")
}
TLVs = append(TLVs, tlv)
}
......
......@@ -2,9 +2,9 @@ package packet
import (
"bytes"
"fmt"
"github.com/bio-routing/bio-rd/protocols/isis/types"
"github.com/pkg/errors"
)
// AreaAddressesTLVType is the type value of an area address TLV
......@@ -29,14 +29,14 @@ func readAreaAddressesTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8) (*A
for read < tlvLength {
areaLen, err := buf.ReadByte()
if err != nil {
return nil, fmt.Errorf("Unable to read: %v", err)
return nil, errors.Wrap(err, "Unable to read")
}
read++
newArea := make(types.AreaID, areaLen)
_, err = buf.Read(newArea)
if err != nil {
return nil, fmt.Errorf("Unable to read: %v", err)
return nil, errors.Wrap(err, "Unable to read")
}
read += areaLen
......
......@@ -2,9 +2,9 @@ package packet
import (
"bytes"
"fmt"
"github.com/bio-routing/bio-rd/util/decode"
"github.com/pkg/errors"
"github.com/taktv6/tflow2/convert"
)
......@@ -45,7 +45,7 @@ func readChecksumTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8) (*Checks
err := decode.Decode(buf, fields)
if err != nil {
return nil, fmt.Errorf("Unable to decode fields: %v", err)
return nil, errors.Wrap(err, "Unable to decode fields")
}
return pdu, nil
......
......@@ -2,9 +2,9 @@ package packet
import (
"bytes"
"fmt"
"github.com/bio-routing/bio-rd/util/decode"
"github.com/pkg/errors"
)
// DynamicHostNameTLVType is the type value of dynamic hostname TLV
......@@ -45,7 +45,7 @@ func readDynamicHostnameTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8) (
err := decode.Decode(buf, fields)
if err != nil {
return nil, fmt.Errorf("Unable to decode fields: %v", err)
return nil, errors.Wrap(err, "Unable to decode fields")
}
return pdu, nil
......
......@@ -2,9 +2,9 @@ package packet
import (
"bytes"
"fmt"
"github.com/bio-routing/bio-rd/util/decode"
"github.com/pkg/errors"
"github.com/taktv6/tflow2/convert"
)
......@@ -39,7 +39,7 @@ func readIPInterfaceAddressTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8
err := decode.Decode(buf, fields)
if err != nil {
return nil, fmt.Errorf("Unable to decode fields: %v", err)
return nil, errors.Wrap(err, "Unable to decode fields")
}
return pdu, nil
......
......@@ -2,10 +2,10 @@ package packet
import (
"bytes"
"fmt"
"github.com/bio-routing/bio-rd/protocols/isis/types"
"github.com/bio-routing/bio-rd/util/decode"
"github.com/pkg/errors"
)
// ISNeighborsTLVType is the type value of an IS Neighbor TLV
......@@ -32,7 +32,7 @@ func readISNeighborsTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8) (*ISN
err := decode.Decode(buf, fields)
if err != nil {
return nil, fmt.Errorf("Unable to decode fields: %v", err)
return nil, errors.Wrap(err, "Unable to decode fields")
}
return pdu, nil
......
......@@ -2,10 +2,10 @@ package packet
import (
"bytes"
"fmt"
"github.com/bio-routing/bio-rd/protocols/isis/types"
"github.com/bio-routing/bio-rd/util/decode"
"github.com/pkg/errors"
"github.com/taktv6/tflow2/convert"
)
......@@ -52,7 +52,7 @@ func readP2PAdjacencyStateTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8)
err := decode.Decode(buf, fields)
if err != nil {
return nil, fmt.Errorf("Unable to decode fields: %v", err)
return nil, errors.Wrap(err, "Unable to decode fields")
}
return pdu, nil
......
......@@ -2,9 +2,9 @@ package packet
import (
"bytes"
"fmt"
"github.com/bio-routing/bio-rd/util/decode"
"github.com/pkg/errors"
)
// ProtocolsSupportedTLVType is the type value of an protocols supported TLV
......@@ -32,7 +32,7 @@ func readProtocolsSupportedTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8
for i := uint8(0); i < tlvLength; i++ {
err := decode.Decode(buf, fields)
if err != nil {
return nil, fmt.Errorf("Unable to decode fields: %v", err)
return nil, errors.Wrap(err, "Unable to decode fields")
}
pdu.NetworkLayerProtocolIDs[i] = protoID
}
......
......@@ -3,6 +3,8 @@ package packet
import (
"bytes"
"fmt"
"github.com/pkg/errors"
)
// UnknownTLV represents an unknown TLV
......@@ -21,7 +23,7 @@ func readUnknownTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8) (*Unknown
n, err := buf.Read(pdu.TLVValue)
if err != nil {
return nil, fmt.Errorf("Unable to read: %v", err)
return nil, errors.Wrap(err, "Unable to read")
}
if n != int(tlvLength) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment