github.com/ethersphere/bee/v2@v2.2.0/pkg/p2p/error.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package p2p
     6  
     7  import (
     8  	"errors"
     9  	"fmt"
    10  	"time"
    11  )
    12  
    13  var (
    14  	// ErrPeerNotFound should be returned by p2p service methods when the requested
    15  	// peer is not found.
    16  	ErrPeerNotFound = errors.New("peer not found")
    17  	// ErrAlreadyConnected is returned if connect was called for already connected node.
    18  	ErrAlreadyConnected = errors.New("already connected")
    19  	// ErrDialLightNode is returned if connect was attempted to a light node.
    20  	ErrDialLightNode = errors.New("target peer is a light node")
    21  	// ErrPeerBlocklisted is returned if peer is on blocklist
    22  	ErrPeerBlocklisted = errors.New("peer blocklisted")
    23  )
    24  
    25  const (
    26  	DefaultBlocklistTime = 1 * time.Minute
    27  )
    28  
    29  // ConnectionBackoffError indicates that connection calls will not be executed until `tryAfter` timestamp.
    30  // The reason is provided in the wrapped error.
    31  type ConnectionBackoffError struct {
    32  	tryAfter time.Time
    33  	err      error
    34  }
    35  
    36  // NewConnectionBackoffError creates new `ConnectionBackoffError` with provided underlying error and `tryAfter` timestamp.
    37  func NewConnectionBackoffError(err error, tryAfter time.Time) error {
    38  	return &ConnectionBackoffError{err: err, tryAfter: tryAfter}
    39  }
    40  
    41  // TryAfter returns a tryAfter timestamp.
    42  func (e *ConnectionBackoffError) TryAfter() time.Time {
    43  	return e.tryAfter
    44  }
    45  
    46  // Unwrap returns an underlying error.
    47  func (e *ConnectionBackoffError) Unwrap() error { return e.err }
    48  
    49  // Error implements function of the standard go error interface.
    50  func (e *ConnectionBackoffError) Error() string {
    51  	return e.err.Error()
    52  }
    53  
    54  // DisconnectError is an error that is specifically handled inside p2p. If returned by specific protocol
    55  // handler it causes peer disconnect.
    56  type DisconnectError struct {
    57  	err error
    58  }
    59  
    60  // NewDisconnectError wraps error and creates a special error that is treated specially
    61  // by p2p. It causes peer to disconnect.
    62  func NewDisconnectError(err error) error {
    63  	return &DisconnectError{
    64  		err: err,
    65  	}
    66  }
    67  
    68  // Unwrap returns an underlying error.
    69  func (e *DisconnectError) Unwrap() error { return e.err }
    70  
    71  // Error implements function of the standard go error interface.
    72  func (e *DisconnectError) Error() string {
    73  	return e.err.Error()
    74  }
    75  
    76  type BlockPeerError struct {
    77  	duration time.Duration
    78  	err      error
    79  }
    80  
    81  // NewBlockPeerError wraps error and creates a special error that is treated specially
    82  // by p2p. It causes peer to be disconnected and blocks any new connection for this peer for the provided duration.
    83  func NewBlockPeerError(duration time.Duration, err error) error {
    84  	return &BlockPeerError{
    85  		duration: duration,
    86  		err:      err,
    87  	}
    88  }
    89  
    90  // Unwrap returns an underlying error.
    91  func (e *BlockPeerError) Unwrap() error { return e.err }
    92  
    93  // Error implements function of the standard go error interface.
    94  func (e *BlockPeerError) Error() string {
    95  	return e.err.Error()
    96  }
    97  
    98  // Duration represents the period for which the peer will be blocked.
    99  // 0 duration is treated as infinity
   100  func (e *BlockPeerError) Duration() time.Duration {
   101  	return e.duration
   102  }
   103  
   104  // IncompatibleStreamError is the error that should be returned by p2p service
   105  // NewStream method when the stream or its version is not supported.
   106  type IncompatibleStreamError struct {
   107  	err error
   108  }
   109  
   110  // NewIncompatibleStreamError wraps the error that is the cause of stream
   111  // incompatibility with IncompatibleStreamError that it can be detected and
   112  // returns it.
   113  func NewIncompatibleStreamError(err error) *IncompatibleStreamError {
   114  	return &IncompatibleStreamError{err: err}
   115  }
   116  
   117  // Unwrap returns an underlying error.
   118  func (e *IncompatibleStreamError) Unwrap() error { return e.err }
   119  
   120  // Error implements function of the standard go error interface.
   121  func (e *IncompatibleStreamError) Error() string {
   122  	return fmt.Sprintf("incompatible stream: %v", e.err)
   123  }