Commit cd37ec88 authored by Janos Guljas's avatar Janos Guljas

organize errors in p2p package and add a test

parent ae1b2782
......@@ -4,6 +4,15 @@
package p2p
import (
"errors"
"fmt"
)
// ErrPeerNotFound should be returned by p2p service methods when the requested
// peer is not found.
var ErrPeerNotFound = errors.New("peer not found")
// DisconnectError is an error that is specifically handled inside p2p. If returned by specific protocol
// handler it causes peer disconnect.
type DisconnectError struct {
......@@ -25,3 +34,24 @@ func (e *DisconnectError) Unwrap() error { return e.err }
func (e *DisconnectError) Error() string {
return e.err.Error()
}
// IncompatibleStreamError is the error that should be returned by p2p service
// NewStream method when the stream or its version is not supported.
type IncompatibleStreamError struct {
err error
}
// NewIncompatibleStreamError wraps the error that is the cause of stream
// incompatibility with IncompatibleStreamError that it can be detected and
// returns it.
func NewIncompatibleStreamError(err error) *IncompatibleStreamError {
return &IncompatibleStreamError{err: err}
}
// Unwrap returns an underlying error.
func (e *IncompatibleStreamError) Unwrap() error { return e.err }
// Error implements function of the standard go error interface.
func (e *IncompatibleStreamError) Error() string {
return fmt.Sprintf("incompatible stream: %v", e.err)
}
......@@ -6,7 +6,6 @@ package p2p
import (
"context"
"fmt"
"io"
"github.com/ethersphere/bee/pkg/swarm"
......@@ -39,23 +38,13 @@ type StreamSpec struct {
Handler HandlerFunc
}
type HandlerFunc func(Peer, Stream) error
type HandlerMiddleware func(HandlerFunc) HandlerFunc
type IncompatibleStreamError struct {
err error
type Peer struct {
Address swarm.Address
}
func NewIncompatibleStreamError(err error) *IncompatibleStreamError {
return &IncompatibleStreamError{err: err}
}
func (e *IncompatibleStreamError) Unwrap() error { return e.err }
type HandlerFunc func(Peer, Stream) error
func (e *IncompatibleStreamError) Error() string {
return fmt.Sprintf("incompatible stream: %v", e.err)
}
type HandlerMiddleware func(HandlerFunc) HandlerFunc
func NewSwarmStreamName(protocol, stream, version string) string {
return "/swarm/" + protocol + "/" + stream + "/" + version
......
......@@ -2,16 +2,19 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package p2p
package p2p_test
import (
"errors"
"testing"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bee/pkg/p2p"
)
type Peer struct {
Address swarm.Address
}
func TestNewSwarmStreamName(t *testing.T) {
want := "/swarm/hive/peers/1.2.0"
got := p2p.NewSwarmStreamName("hive", "peers", "1.2.0")
var ErrPeerNotFound = errors.New("peer not found")
if got != want {
t.Errorf("got %s, want %s", got, want)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment