decred.org/dcrdex@v1.0.5/server/db/driver/pg/errors.go (about)

     1  // This code is available on the terms of the project LICENSE.md file,
     2  // also available online at https://blueoakcouncil.org/license/1.0.0.
     3  
     4  package pg
     5  
     6  import (
     7  	"errors"
     8  )
     9  
    10  // These errors are specific to the pg backend; they are not generic DEX
    11  // archivist errors.
    12  var (
    13  	errNoRows      = errors.New("no rows")
    14  	errTooManyRows = errors.New("too many rows")
    15  )
    16  
    17  // DetailedError pairs an Error with details.
    18  type DetailedError struct {
    19  	wrapped error
    20  	detail  string
    21  }
    22  
    23  // Error satisfies the error interface, combining the wrapped error message with
    24  // the details.
    25  func (e DetailedError) Error() string {
    26  	return e.wrapped.Error() + ": " + e.detail
    27  }
    28  
    29  // Unwrap returns the wrapped error, allowing errors.Is and errors.As to work.
    30  func (e DetailedError) Unwrap() error {
    31  	return e.wrapped
    32  }
    33  
    34  // NewDetailedError wraps the provided Error with details in a DetailedError,
    35  // facilitating the use of errors.Is and errors.As via errors.Unwrap.
    36  func NewDetailedError(err error, detail string) DetailedError {
    37  	return DetailedError{
    38  		wrapped: err,
    39  		detail:  detail,
    40  	}
    41  }