error.go raw

   1  // Copyright (c) 2013-2015 The btcsuite 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 wire
   6  
   7  import (
   8  	"fmt"
   9  )
  10  
  11  // MessageError describes an issue with a message.
  12  // An example of some potential issues are messages from the wrong bitcoin
  13  // network, invalid commands, mismatched checksums, and exceeding max payloads.
  14  //
  15  // This provides a mechanism for the caller to type assert the error to
  16  // differentiate between general io errors such as io.EOF and issues that
  17  // resulted from malformed messages.
  18  type MessageError struct {
  19  	Func        string // Function name
  20  	Description string // Human readable description of the issue
  21  }
  22  
  23  // Error satisfies the error interface and prints human-readable errors.
  24  func (e *MessageError) Error() string {
  25  	if e.Func != "" {
  26  		return fmt.Sprintf("%v: %v", e.Func, e.Description)
  27  	}
  28  	return e.Description
  29  }
  30  
  31  // messageError creates an error for the given function and description.
  32  func messageError(f string, desc string) *MessageError {
  33  	return &MessageError{Func: f, Description: desc}
  34  }
  35