github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/error/error.go (about)

     1  /* For license and copyright information please see the LEGAL file in the code repository */
     2  
     3  package error
     4  
     5  import (
     6  	"github.com/GeniusesGroup/libgo/detail"
     7  	"github.com/GeniusesGroup/libgo/mediatype"
     8  	"github.com/GeniusesGroup/libgo/protocol"
     9  )
    10  
    11  // New return new Error that implement protocol.Error
    12  // Never change MediaType due to it adds unnecessary complicated troubleshooting errors on SDK.
    13  // TODO::: escapes to heap problem of return value, How prevent it??
    14  // func New(mediatype string) (err Error) { err.Init(mediatype); return }
    15  
    16  // Err is the same as the Error.
    17  // Use this type when embed in other struct to solve field & method same name problem(Error struct and Error() method) to satisfy interfaces.
    18  type Err = Error
    19  
    20  // Error implements protocol.Error
    21  type Error struct {
    22  	internal  bool
    23  	temporary bool
    24  
    25  	detail.DS
    26  	mediatype.MT
    27  }
    28  
    29  // Init initialize Error that implement protocol.Error
    30  // Never change MediaType due to it adds unnecessary complicated troubleshooting errors on SDK.
    31  func (e *Error) Init(mediatype string) {
    32  	e.MT.Init(mediatype)
    33  
    34  	// RegisterError will register in the application.
    35  	// Force to check by runtime check, due to testing package not let us by any const!
    36  	if protocol.App != nil {
    37  		protocol.App.RegisterError(e)
    38  	}
    39  }
    40  
    41  // Equal compare two Error.
    42  func (e *Error) Equal(err protocol.Error) bool {
    43  	if e == nil && err == nil {
    44  		return true
    45  	}
    46  	if e != nil && err != nil && e.ID() == err.ID() {
    47  		return true
    48  	}
    49  	// TODO::: check err as chain error
    50  	return false
    51  }
    52  
    53  func (e *Error) Internal() bool  { return e.internal }
    54  func (e *Error) Temporary() bool { return e.temporary }
    55  
    56  func (e *Error) SetInternal()  { e.internal = true }
    57  func (e *Error) SetTemporary() { e.temporary = true }
    58  
    59  func (e *Error) Notify() {
    60  	// TODO:::
    61  }
    62  
    63  // Go compatibility methods. Unwrap provides compatibility for Go 1.13 error chains.
    64  func (e *Error) Error() string { return e.MT.MediaType() }
    65  func (e *Error) Cause() error  { return nil }
    66  func (e *Error) Unwrap() error { return nil }
    67  // func (e *Error) Is(error) bool
    68  // func (e *Error) As(any) bool