github.com/blend/go-sdk@v1.20240719.1/testutil/errors.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package testutil
     9  
    10  // NOTE: Ensure that
    11  //   - `SingleError` satisfies `ErrorProducer`.
    12  //   - `SliceErrors` satisfies `ErrorProducer`.
    13  var (
    14  	_ ErrorProducer = (*SingleError)(nil)
    15  	_ ErrorProducer = (*SliceErrors)(nil)
    16  )
    17  
    18  // ErrorProducer is an interface that defines an error factory.
    19  type ErrorProducer interface {
    20  	NextError() error
    21  }
    22  
    23  // SingleError satisfies ErrorProducer for a single error.
    24  type SingleError struct {
    25  	Error error
    26  }
    27  
    28  // NextError produces the "next" error.
    29  func (se *SingleError) NextError() error {
    30  	return se.Error
    31  }
    32  
    33  // SliceErrors satisfies ErrorProducer for a slice of errors.
    34  type SliceErrors struct {
    35  	Errors []error
    36  	Index  int
    37  }
    38  
    39  // NextError produces the "next" error. (This is not concurrency safe.)
    40  func (se *SliceErrors) NextError() error {
    41  	index := se.Index
    42  	se.Index++
    43  
    44  	if index < 0 {
    45  		return nil
    46  	}
    47  
    48  	if index >= len(se.Errors) {
    49  		return nil
    50  	}
    51  
    52  	return se.Errors[index]
    53  }