Skip to content

Commit 2ece159

Browse files
committed
common: use more precise errors and move them to their respective package
1 parent 26b9ab9 commit 2ece159

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+115
-128
lines changed

analyzer/server.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ func NewServerFromConfig() (*Server, error) {
296296
StatusReporter: s,
297297
TLSConfig: tlsConfig,
298298
Peers: peers,
299-
EtcdKeysAPI: etcdClient.KeysAPI,
300299
TopologyMarshallers: api.TopologyMarshallers,
301300
}
302301

@@ -339,13 +338,13 @@ func NewServerFromConfig() (*Server, error) {
339338
flowSubscriberWSServer := ws.NewStructServer(config.NewWSServer(hub.HTTPServer(), "/ws/subscriber/flow", apiAuthBackend))
340339
flowSubscriberEndpoint := server.NewFlowSubscriberEndpoint(flowSubscriberWSServer)
341340

342-
captureAPIHandler, err := api.RegisterCaptureAPI(hub.APIServer(), g, apiAuthBackend)
341+
captureAPIHandler, err := api.RegisterCaptureAPI(hub.APIServer(), etcdClient.KeysAPI, g, apiAuthBackend)
343342
if err != nil {
344343
return nil, err
345344
}
346345

347346
apiServer := hub.APIServer()
348-
piAPIHandler, err := api.RegisterPacketInjectorAPI(g, apiServer, apiAuthBackend)
347+
piAPIHandler, err := api.RegisterPacketInjectorAPI(g, apiServer, etcdClient.KeysAPI, apiAuthBackend)
349348
if err != nil {
350349
return nil, err
351350
}
@@ -357,7 +356,7 @@ func NewServerFromConfig() (*Server, error) {
357356
return nil, err
358357
}
359358

360-
nodeRuleAPIHandler, err := api.RegisterNodeRuleAPI(apiServer, g, apiAuthBackend)
359+
nodeRuleAPIHandler, err := api.RegisterNodeRuleAPI(apiServer, etcdClient.KeysAPI, g, apiAuthBackend)
361360
if err != nil {
362361
return nil, err
363362
}
@@ -367,18 +366,18 @@ func NewServerFromConfig() (*Server, error) {
367366
return nil, err
368367
}
369368

370-
edgeRuleAPIHandler, err := api.RegisterEdgeRuleAPI(apiServer, g, apiAuthBackend)
369+
edgeRuleAPIHandler, err := api.RegisterEdgeRuleAPI(apiServer, etcdClient.KeysAPI, g, apiAuthBackend)
371370
if err != nil {
372371
return nil, err
373372
}
374373

375374
s.topologyManager = usertopology.NewTopologyManager(etcdClient, nodeRuleAPIHandler, edgeRuleAPIHandler, g)
376375

377-
if _, err = api.RegisterAlertAPI(apiServer, apiAuthBackend); err != nil {
376+
if _, err = api.RegisterAlertAPI(apiServer, etcdClient.KeysAPI, apiAuthBackend); err != nil {
378377
return nil, err
379378
}
380379

381-
if _, err := api.RegisterWorkflowAPI(apiServer, apiAuthBackend); err != nil {
380+
if _, err := api.RegisterWorkflowAPI(apiServer, etcdClient.KeysAPI, apiAuthBackend); err != nil {
382381
return nil, err
383382
}
384383

api/client/gremlin.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ package client
2020
import (
2121
"bytes"
2222
"encoding/json"
23+
"errors"
2324
"fmt"
2425
"io/ioutil"
2526
"net/http"
2627

2728
"github.com/skydive-project/skydive/api/types"
28-
"github.com/skydive-project/skydive/common"
2929
"github.com/skydive-project/skydive/flow"
3030
"github.com/skydive-project/skydive/graffiti/graph"
3131
shttp "github.com/skydive-project/skydive/graffiti/http"
@@ -35,6 +35,9 @@ import (
3535
"github.com/skydive-project/skydive/topology/probes/socketinfo"
3636
)
3737

38+
// ErrNoResult is returned when a query returned no result
39+
var ErrNoResult = errors.New("no result")
40+
3841
// GremlinQueryHelper describes a gremlin query request query helper mechanism
3942
type GremlinQueryHelper struct {
4043
authOptions *shttp.AuthenticationOpts
@@ -137,7 +140,7 @@ func (g *GremlinQueryHelper) GetNode(query interface{}) (node *graph.Node, _ err
137140
return nodes[0], nil
138141
}
139142

140-
return nil, common.ErrNotFound
143+
return nil, ErrNoResult
141144
}
142145

143146
// GetFlows from the Gremlin query
@@ -206,7 +209,7 @@ func (g *GremlinQueryHelper) GetFlowMetrics(query interface{}) (map[string][]*fl
206209
}
207210

208211
if len(result) == 0 {
209-
return nil, common.ErrNotFound
212+
return nil, ErrNoResult
210213
}
211214

212215
return result[0], nil

api/server/alert.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package server
2323
import (
2424
"time"
2525

26+
etcd "github.com/coreos/etcd/client"
2627
"github.com/skydive-project/skydive/api/types"
2728
"github.com/skydive-project/skydive/graffiti/api/rest"
2829
api "github.com/skydive-project/skydive/graffiti/api/server"
@@ -52,11 +53,11 @@ func (a *AlertResourceHandler) Name() string {
5253
}
5354

5455
// RegisterAlertAPI registers an Alert's API to a designated API Server
55-
func RegisterAlertAPI(apiServer *api.Server, authBackend shttp.AuthenticationBackend) (*AlertAPIHandler, error) {
56+
func RegisterAlertAPI(apiServer *api.Server, kapi etcd.KeysAPI, authBackend shttp.AuthenticationBackend) (*AlertAPIHandler, error) {
5657
alertAPIHandler := &AlertAPIHandler{
5758
BasicAPIHandler: rest.BasicAPIHandler{
5859
ResourceHandler: &AlertResourceHandler{},
59-
EtcdKeyAPI: apiServer.EtcdKeyAPI,
60+
EtcdKeyAPI: kapi,
6061
},
6162
}
6263
if err := apiServer.RegisterAPIHandler(alertAPIHandler, authBackend); err != nil {

api/server/capture.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package server
2323
import (
2424
"fmt"
2525

26+
etcd "github.com/coreos/etcd/client"
2627
"github.com/skydive-project/skydive/api/types"
2728
"github.com/skydive-project/skydive/flow"
2829
"github.com/skydive-project/skydive/flow/probes"
@@ -146,11 +147,11 @@ func (c *CaptureAPIHandler) Create(r rest.Resource, opts *rest.CreateOptions) er
146147
}
147148

148149
// RegisterCaptureAPI registers an new resource, capture
149-
func RegisterCaptureAPI(apiServer *api.Server, g *graph.Graph, authBackend shttp.AuthenticationBackend) (*CaptureAPIHandler, error) {
150+
func RegisterCaptureAPI(apiServer *api.Server, kapi etcd.KeysAPI, g *graph.Graph, authBackend shttp.AuthenticationBackend) (*CaptureAPIHandler, error) {
150151
captureAPIHandler := &CaptureAPIHandler{
151152
BasicAPIHandler: rest.BasicAPIHandler{
152153
ResourceHandler: &CaptureResourceHandler{},
153-
EtcdKeyAPI: apiServer.EtcdKeyAPI,
154+
EtcdKeyAPI: kapi,
154155
},
155156
Graph: g,
156157
}

api/server/edge.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"time"
2525

2626
"github.com/skydive-project/skydive/api/types"
27-
"github.com/skydive-project/skydive/common"
2827
"github.com/skydive-project/skydive/graffiti/api/rest"
2928
api "github.com/skydive-project/skydive/graffiti/api/server"
3029
"github.com/skydive-project/skydive/graffiti/graph"
@@ -110,7 +109,7 @@ func (h *EdgeAPIHandler) Delete(id string) error {
110109

111110
edge := h.g.GetEdge(graph.Identifier(id))
112111
if edge == nil {
113-
return common.ErrNotFound
112+
return rest.ErrNotFound
114113
}
115114

116115
return h.g.DelEdge(edge)

api/server/edgerule.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package server
2222

2323
import (
24+
etcd "github.com/coreos/etcd/client"
2425
"github.com/skydive-project/skydive/api/types"
2526
"github.com/skydive-project/skydive/graffiti/api/rest"
2627
api "github.com/skydive-project/skydive/graffiti/api/server"
@@ -50,11 +51,11 @@ func (erh *EdgeRuleResourceHandler) New() rest.Resource {
5051
}
5152

5253
// RegisterEdgeRuleAPI registers an EdgeRule's API to a designated API Server
53-
func RegisterEdgeRuleAPI(apiServer *api.Server, g *graph.Graph, authBackend shttp.AuthenticationBackend) (*EdgeRuleAPI, error) {
54+
func RegisterEdgeRuleAPI(apiServer *api.Server, kapi etcd.KeysAPI, g *graph.Graph, authBackend shttp.AuthenticationBackend) (*EdgeRuleAPI, error) {
5455
era := &EdgeRuleAPI{
5556
BasicAPIHandler: rest.BasicAPIHandler{
5657
ResourceHandler: &EdgeRuleResourceHandler{},
57-
EtcdKeyAPI: apiServer.EtcdKeyAPI,
58+
EtcdKeyAPI: kapi,
5859
},
5960
Graph: g,
6061
}

api/server/node.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"time"
2525

2626
"github.com/skydive-project/skydive/api/types"
27-
"github.com/skydive-project/skydive/common"
2827
"github.com/skydive-project/skydive/graffiti/api/rest"
2928
api "github.com/skydive-project/skydive/graffiti/api/server"
3029
"github.com/skydive-project/skydive/graffiti/graph"
@@ -110,7 +109,7 @@ func (h *NodeAPIHandler) Delete(id string) error {
110109

111110
node := h.g.GetNode(graph.Identifier(id))
112111
if node == nil {
113-
return common.ErrNotFound
112+
return rest.ErrNotFound
114113
}
115114

116115
return h.g.DelNode(node)

api/server/noderule.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package server
2222

2323
import (
24+
etcd "github.com/coreos/etcd/client"
2425
"github.com/skydive-project/skydive/api/types"
2526
"github.com/skydive-project/skydive/graffiti/api/rest"
2627
api "github.com/skydive-project/skydive/graffiti/api/server"
@@ -50,11 +51,11 @@ func (nrh *NodeRuleResourceHandler) New() rest.Resource {
5051
}
5152

5253
// RegisterNodeRuleAPI register a new node rule api handler
53-
func RegisterNodeRuleAPI(apiServer *api.Server, g *graph.Graph, authBackend shttp.AuthenticationBackend) (*NodeRuleAPI, error) {
54+
func RegisterNodeRuleAPI(apiServer *api.Server, kapi etcd.KeysAPI, g *graph.Graph, authBackend shttp.AuthenticationBackend) (*NodeRuleAPI, error) {
5455
nra := &NodeRuleAPI{
5556
BasicAPIHandler: rest.BasicAPIHandler{
5657
ResourceHandler: &NodeRuleResourceHandler{},
57-
EtcdKeyAPI: apiServer.EtcdKeyAPI,
58+
EtcdKeyAPI: kapi,
5859
},
5960
Graph: g,
6061
}

api/server/packet_injector_common.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package server
2222

2323
import (
24+
etcd "github.com/coreos/etcd/client"
2425
"github.com/skydive-project/skydive/api/types"
2526
"github.com/skydive-project/skydive/graffiti/api/rest"
2627
api "github.com/skydive-project/skydive/graffiti/api/server"
@@ -76,11 +77,11 @@ func (pi *PacketInjectorAPI) getNode(gremlinQuery string) *graph.Node {
7677
}
7778

7879
// RegisterPacketInjectorAPI registers a new packet injector resource in the API
79-
func RegisterPacketInjectorAPI(g *graph.Graph, apiServer *api.Server, authBackend shttp.AuthenticationBackend) (*PacketInjectorAPI, error) {
80+
func RegisterPacketInjectorAPI(g *graph.Graph, apiServer *api.Server, kapi etcd.KeysAPI, authBackend shttp.AuthenticationBackend) (*PacketInjectorAPI, error) {
8081
pia := &PacketInjectorAPI{
8182
BasicAPIHandler: rest.BasicAPIHandler{
8283
ResourceHandler: &packetInjectorResourceHandler{},
83-
EtcdKeyAPI: apiServer.EtcdKeyAPI,
84+
EtcdKeyAPI: kapi,
8485
},
8586
Graph: g,
8687
}

api/server/workflow.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
yaml "gopkg.in/yaml.v2"
2727

28+
etcd "github.com/coreos/etcd/client"
2829
"github.com/skydive-project/skydive/api/types"
2930
"github.com/skydive-project/skydive/graffiti/api/rest"
3031
api "github.com/skydive-project/skydive/graffiti/api/server"
@@ -110,11 +111,11 @@ func (w *WorkflowAPIHandler) Index() map[string]rest.Resource {
110111
}
111112

112113
// RegisterWorkflowAPI registers a new workflow api handler
113-
func RegisterWorkflowAPI(apiServer *api.Server, authBackend shttp.AuthenticationBackend) (*WorkflowAPIHandler, error) {
114+
func RegisterWorkflowAPI(apiServer *api.Server, kapi etcd.KeysAPI, authBackend shttp.AuthenticationBackend) (*WorkflowAPIHandler, error) {
114115
workflowAPIHandler := &WorkflowAPIHandler{
115116
BasicAPIHandler: rest.BasicAPIHandler{
116117
ResourceHandler: &WorkflowResourceHandler{},
117-
EtcdKeyAPI: apiServer.EtcdKeyAPI,
118+
EtcdKeyAPI: kapi,
118119
},
119120
}
120121
if err := apiServer.RegisterAPIHandler(workflowAPIHandler, authBackend); err != nil {

common/types.go

-17
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,6 @@
1717

1818
package common
1919

20-
import (
21-
"errors"
22-
)
23-
24-
var (
25-
// ErrCantCompareInterface error can't compare interface
26-
ErrCantCompareInterface = errors.New("Can't compare interface")
27-
// ErrFieldWrongType error field has wrong type
28-
ErrFieldWrongType = errors.New("Field has wrong type")
29-
// ErrNotFound error no result was found
30-
ErrNotFound = errors.New("No result found")
31-
// ErrTimeout network timeout
32-
ErrTimeout = errors.New("Timeout")
33-
// ErrNotImplemented unimplemented feature
34-
ErrNotImplemented = errors.New("Not implemented")
35-
)
36-
3720
// SortOrder describes ascending or descending order
3821
type SortOrder string
3922

flow/no_bpf.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,22 @@
2020
package flow
2121

2222
import (
23+
"errors"
24+
2325
"github.com/google/gopacket/layers"
2426
"golang.org/x/net/bpf"
25-
26-
"github.com/skydive-project/skydive/common"
2727
)
2828

29+
var errNeedPcap = errors.New("BPF filtering needs libpcap support")
30+
2931
// BPF describes a filter
3032
type BPF struct {
3133
vm *bpf.VM
3234
}
3335

3436
// BPFFilterToRaw creates a raw binary filter from a BPF expression
3537
func BPFFilterToRaw(linkType layers.LinkType, captureLength uint32, filter string) ([]bpf.RawInstruction, error) {
36-
return nil, common.ErrNotImplemented
38+
return nil, errNeedPcap
3739
}
3840

3941
// Matches returns true data match the filter
@@ -43,5 +45,5 @@ func (b *BPF) Matches(data []byte) bool {
4345

4446
// NewBPF creates a new BPF filter
4547
func NewBPF(linkType layers.LinkType, captureLength uint32, filter string) (*BPF, error) {
46-
return nil, common.ErrNotImplemented
48+
return nil, errNeedPcap
4749
}

flow/probes/gopacket/no_gopacket.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@
2020
package gopacket
2121

2222
import (
23-
"github.com/skydive-project/skydive/common"
2423
"github.com/skydive-project/skydive/flow/probes"
2524
"github.com/skydive-project/skydive/probe"
2625
)
2726

2827
// NewProbe returns a new GoPacket probe
2928
func NewProbe(ctx probes.Context, bundle *probe.Bundle) (probes.FlowProbeHandler, error) {
30-
return nil, common.ErrNotImplemented
29+
return nil, probe.ErrNotImplemented
3130
}

flow/probes/ovsmirror/no_ovsmirror.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@
2020
package ovsmirror
2121

2222
import (
23-
"github.com/skydive-project/skydive/common"
2423
"github.com/skydive-project/skydive/flow/probes"
2524
"github.com/skydive-project/skydive/probe"
2625
)
2726

2827
// NewProbe returns an empty flow probe handler and an error
2928
func NewProbe(ctx probes.Context, bundle *probe.Bundle) (probes.FlowProbeHandler, error) {
30-
return nil, common.ErrNotImplemented
29+
return nil, probe.ErrNotImplemented
3130
}

flow/probes/targets/no_erspan.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ package targets
2222
import (
2323
"github.com/google/gopacket"
2424
"github.com/skydive-project/skydive/api/types"
25-
"github.com/skydive-project/skydive/common"
2625
"github.com/skydive-project/skydive/flow"
2726
"github.com/skydive-project/skydive/graffiti/graph"
2827
)
@@ -45,5 +44,5 @@ func (ers *ERSpanTarget) Stop() {
4544

4645
// NewERSpanTarget returns a new ERSpan target
4746
func NewERSpanTarget(g *graph.Graph, n *graph.Node, capture *types.Capture) (*ERSpanTarget, error) {
48-
return nil, common.ErrNotImplemented
47+
return nil, errrors.ErrNotImplemented
4948
}

graffiti/api/rest/kv.go

+3
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ func (h *BasicAPIHandler) Delete(id string) error {
127127
etcdPath := fmt.Sprintf("/%s/%s", h.ResourceHandler.Name(), id)
128128

129129
_, err := h.EtcdKeyAPI.Delete(context.Background(), etcdPath, nil)
130+
if err, ok := err.(etcd.Error); ok && err.Code == etcd.ErrorCodeKeyNotFound {
131+
return ErrNotFound
132+
}
130133
return err
131134
}
132135

graffiti/api/rest/rest.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ import (
2323
)
2424

2525
// ErrDuplicatedResource is returned when a resource is duplicated
26-
var ErrDuplicatedResource = errors.New("Duplicated resource")
26+
var ErrDuplicatedResource = errors.New("duplicated resource")
27+
28+
// ErrNotFound is returned when a resource could not be found
29+
var ErrNotFound = errors.New("resource not found")
2730

2831
// Resource used as interface resources for each API
2932
type Resource interface {

0 commit comments

Comments
 (0)