[PD1] Error handling project-wide

This commit is contained in:
Afonso Franco 2024-04-28 22:02:13 +01:00
parent f5b3726673
commit b918211736
Signed by: afonso
SSH key fingerprint: SHA256:aiLbdlPwXKJS5wMnghdtod0SPy8imZjlVvCyUX9DJNk
13 changed files with 364 additions and 245 deletions

View file

@ -22,33 +22,27 @@ func NewConnection[T any](netConn *tls.Conn) Connection[T] {
}
}
func (c Connection[T]) Send(obj T) bool {
func (c Connection[T]) Send(obj T) error {
if err := c.encoder.Encode(&obj); err!=nil {
if err == io.EOF {
log.Println("Connection closed by peer")
//Return false as connection not active
return false
} else {
log.Panic(err)
}
}
return err
}
//Return true as connection active
return true
return nil
}
func (c Connection[T]) Receive() (*T, bool) {
func (c Connection[T]) Receive() (*T, error) {
var obj T
if err := c.decoder.Decode(&obj); err != nil {
if err == io.EOF {
log.Println("Connection closed by peer")
//Return false as connection not active
return nil,false
} else {
log.Panic(err)
}
}
}
return nil,err
}
//Return true as connection active
return &obj, true
return &obj, nil
}
func (c Connection[T]) GetPeerCertificate() *x509.Certificate {