error.go raw

   1  // Copyright (c) 2024 The Decred developers
   2  // Use of this source code is governed by an ISC
   3  // license that can be found in the LICENSE file.
   4  
   5  package blake256
   6  
   7  // ErrorKind identifies a kind of error.
   8  type ErrorKind string
   9  
  10  // These constants are used to identify a specific ErrorKind.
  11  const (
  12  	// ErrMalformedState indicates a serialized intermediate state is malformed
  13  	// in some way such as not having at least the expected number of bytes.
  14  	ErrMalformedState = ErrorKind("ErrMalformedState")
  15  
  16  	// ErrMismatchedState indicates a serialized intermediate state is not for
  17  	// the hash type that is attempting to restore it.  For example, it will be
  18  	// returned when attempting to restore a BLAKE-256 intermediate state with
  19  	// a BLAKE-224 hasher.
  20  	ErrMismatchedState = ErrorKind("ErrMismatchedState")
  21  )
  22  
  23  // Error satisfies the error interface and prints human-readable errors.
  24  func (e ErrorKind) Error() string {
  25  	return string(e)
  26  }
  27  
  28  // Error identifies an error related to restoring an intermediate hashing state.
  29  //
  30  // It has full support for [errors.Is] and [errors.As], so the caller can
  31  // ascertain the specific reason for the error by checking the underlying error.
  32  type Error struct {
  33  	Err         error
  34  	Description string
  35  }
  36  
  37  // Error satisfies the error interface and prints human-readable errors.
  38  func (e Error) Error() string {
  39  	return e.Description
  40  }
  41  
  42  // Unwrap returns the underlying wrapped error.
  43  func (e Error) Unwrap() error {
  44  	return e.Err
  45  }
  46  
  47  // makeError creates an [Error] given a set of arguments.
  48  func makeError(kind ErrorKind, desc string) Error {
  49  	return Error{Err: kind, Description: desc}
  50  }
  51