github.com/MetalBlockchain/metalgo@v1.11.9/snow/engine/common/error.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package common
     5  
     6  import "fmt"
     7  
     8  var (
     9  	_ error = (*AppError)(nil)
    10  
    11  	// ErrUndefined indicates an undefined error
    12  	ErrUndefined = &AppError{
    13  		Code:    0,
    14  		Message: "undefined",
    15  	}
    16  
    17  	// ErrTimeout is used to signal a response timeout
    18  	ErrTimeout = &AppError{
    19  		Code:    -1,
    20  		Message: "timed out",
    21  	}
    22  )
    23  
    24  // AppError is an application-defined error
    25  type AppError struct {
    26  	// Code is application-defined and should be used for error matching
    27  	Code int32
    28  	// Message is a human-readable error message
    29  	Message string
    30  }
    31  
    32  func (a *AppError) Error() string {
    33  	return fmt.Sprintf("%d: %s", a.Code, a.Message)
    34  }
    35  
    36  func (a *AppError) Is(target error) bool {
    37  	appErr, ok := target.(*AppError)
    38  	if !ok {
    39  		return false
    40  	}
    41  
    42  	return a.Code == appErr.Code
    43  }