github.com/pion/dtls/v2@v2.2.12/pkg/protocol/errors.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  package protocol
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"net"
    10  )
    11  
    12  var (
    13  	errBufferTooSmall    = &TemporaryError{Err: errors.New("buffer is too small")} //nolint:goerr113
    14  	errInvalidCipherSpec = &FatalError{Err: errors.New("cipher spec invalid")}     //nolint:goerr113
    15  )
    16  
    17  // FatalError indicates that the DTLS connection is no longer available.
    18  // It is mainly caused by wrong configuration of server or client.
    19  type FatalError struct {
    20  	Err error
    21  }
    22  
    23  // InternalError indicates and internal error caused by the implementation, and the DTLS connection is no longer available.
    24  // It is mainly caused by bugs or tried to use unimplemented features.
    25  type InternalError struct {
    26  	Err error
    27  }
    28  
    29  // TemporaryError indicates that the DTLS connection is still available, but the request was failed temporary.
    30  type TemporaryError struct {
    31  	Err error
    32  }
    33  
    34  // TimeoutError indicates that the request was timed out.
    35  type TimeoutError struct {
    36  	Err error
    37  }
    38  
    39  // HandshakeError indicates that the handshake failed.
    40  type HandshakeError struct {
    41  	Err error
    42  }
    43  
    44  // Timeout implements net.Error.Timeout()
    45  func (*FatalError) Timeout() bool { return false }
    46  
    47  // Temporary implements net.Error.Temporary()
    48  func (*FatalError) Temporary() bool { return false }
    49  
    50  // Unwrap implements Go1.13 error unwrapper.
    51  func (e *FatalError) Unwrap() error { return e.Err }
    52  
    53  func (e *FatalError) Error() string { return fmt.Sprintf("dtls fatal: %v", e.Err) }
    54  
    55  // Timeout implements net.Error.Timeout()
    56  func (*InternalError) Timeout() bool { return false }
    57  
    58  // Temporary implements net.Error.Temporary()
    59  func (*InternalError) Temporary() bool { return false }
    60  
    61  // Unwrap implements Go1.13 error unwrapper.
    62  func (e *InternalError) Unwrap() error { return e.Err }
    63  
    64  func (e *InternalError) Error() string { return fmt.Sprintf("dtls internal: %v", e.Err) }
    65  
    66  // Timeout implements net.Error.Timeout()
    67  func (*TemporaryError) Timeout() bool { return false }
    68  
    69  // Temporary implements net.Error.Temporary()
    70  func (*TemporaryError) Temporary() bool { return true }
    71  
    72  // Unwrap implements Go1.13 error unwrapper.
    73  func (e *TemporaryError) Unwrap() error { return e.Err }
    74  
    75  func (e *TemporaryError) Error() string { return fmt.Sprintf("dtls temporary: %v", e.Err) }
    76  
    77  // Timeout implements net.Error.Timeout()
    78  func (*TimeoutError) Timeout() bool { return true }
    79  
    80  // Temporary implements net.Error.Temporary()
    81  func (*TimeoutError) Temporary() bool { return true }
    82  
    83  // Unwrap implements Go1.13 error unwrapper.
    84  func (e *TimeoutError) Unwrap() error { return e.Err }
    85  
    86  func (e *TimeoutError) Error() string { return fmt.Sprintf("dtls timeout: %v", e.Err) }
    87  
    88  // Timeout implements net.Error.Timeout()
    89  func (e *HandshakeError) Timeout() bool {
    90  	var netErr net.Error
    91  	if errors.As(e.Err, &netErr) {
    92  		return netErr.Timeout()
    93  	}
    94  	return false
    95  }
    96  
    97  // Temporary implements net.Error.Temporary()
    98  func (e *HandshakeError) Temporary() bool {
    99  	var netErr net.Error
   100  	if errors.As(e.Err, &netErr) {
   101  		return netErr.Temporary() //nolint
   102  	}
   103  	return false
   104  }
   105  
   106  // Unwrap implements Go1.13 error unwrapper.
   107  func (e *HandshakeError) Unwrap() error { return e.Err }
   108  
   109  func (e *HandshakeError) Error() string { return fmt.Sprintf("handshake error: %v", e.Err) }