github.com/aavshr/aws-sdk-go@v1.41.3/private/protocol/eventstream/eventstreamapi/error.go (about)

     1  package eventstreamapi
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  )
     7  
     8  // InputWriterCloseErrorCode is used to denote an error occurred
     9  // while closing the event stream input writer.
    10  const InputWriterCloseErrorCode = "EventStreamInputWriterCloseError"
    11  
    12  type messageError struct {
    13  	code string
    14  	msg  string
    15  }
    16  
    17  func (e messageError) Code() string {
    18  	return e.code
    19  }
    20  
    21  func (e messageError) Message() string {
    22  	return e.msg
    23  }
    24  
    25  func (e messageError) Error() string {
    26  	return fmt.Sprintf("%s: %s", e.code, e.msg)
    27  }
    28  
    29  func (e messageError) OrigErr() error {
    30  	return nil
    31  }
    32  
    33  // OnceError wraps the behavior of recording an error
    34  // once and signal on a channel when this has occurred.
    35  // Signaling is done by closing of the channel.
    36  //
    37  // Type is safe for concurrent usage.
    38  type OnceError struct {
    39  	mu  sync.RWMutex
    40  	err error
    41  	ch  chan struct{}
    42  }
    43  
    44  // NewOnceError return a new OnceError
    45  func NewOnceError() *OnceError {
    46  	return &OnceError{
    47  		ch: make(chan struct{}, 1),
    48  	}
    49  }
    50  
    51  // Err acquires a read-lock and returns an
    52  // error if one has been set.
    53  func (e *OnceError) Err() error {
    54  	e.mu.RLock()
    55  	err := e.err
    56  	e.mu.RUnlock()
    57  
    58  	return err
    59  }
    60  
    61  // SetError acquires a write-lock and will set
    62  // the underlying error value if one has not been set.
    63  func (e *OnceError) SetError(err error) {
    64  	if err == nil {
    65  		return
    66  	}
    67  
    68  	e.mu.Lock()
    69  	if e.err == nil {
    70  		e.err = err
    71  		close(e.ch)
    72  	}
    73  	e.mu.Unlock()
    74  }
    75  
    76  // ErrorSet returns a channel that will be used to signal
    77  // that an error has been set. This channel will be closed
    78  // when the error value has been set for OnceError.
    79  func (e *OnceError) ErrorSet() <-chan struct{} {
    80  	return e.ch
    81  }