Improved error messages

This commit is contained in:
Cristian Maglie
2024-02-16 14:17:53 +01:00
parent 1d3c0099c3
commit 9aae282f09

View File

@@ -9,6 +9,7 @@
package serial
import (
"fmt"
"io/ioutil"
"regexp"
"strings"
@@ -232,7 +233,7 @@ func nativeOpen(portName string, mode *Mode) (*unixPort, error) {
settings, err := port.getTermSettings()
if err != nil {
port.Close()
return nil, &PortError{code: InvalidSerialPort}
return nil, &PortError{code: InvalidSerialPort, causedBy: fmt.Errorf("error getting term settings: %w", err)}
}
// Set raw mode
@@ -243,14 +244,14 @@ func nativeOpen(portName string, mode *Mode) (*unixPort, error) {
if port.setTermSettings(settings) != nil {
port.Close()
return nil, &PortError{code: InvalidSerialPort}
return nil, &PortError{code: InvalidSerialPort, causedBy: fmt.Errorf("error setting term settings: %w", err)}
}
if mode.InitialStatusBits != nil {
status, err := port.getModemBitsStatus()
if err != nil {
port.Close()
return nil, &PortError{code: InvalidSerialPort, causedBy: err}
return nil, &PortError{code: InvalidSerialPort, causedBy: fmt.Errorf("error getting modem bits status: %w", err)}
}
if mode.InitialStatusBits.DTR {
status |= unix.TIOCM_DTR
@@ -264,15 +265,15 @@ func nativeOpen(portName string, mode *Mode) (*unixPort, error) {
}
if err := port.setModemBitsStatus(status); err != nil {
port.Close()
return nil, &PortError{code: InvalidSerialPort, causedBy: err}
return nil, &PortError{code: InvalidSerialPort, causedBy: fmt.Errorf("error setting modem bits status: %w", err)}
}
}
// MacOSX require that this operation is the last one otherwise an
// 'Invalid serial port' error is returned... don't know why...
if port.SetMode(mode) != nil {
if err := port.SetMode(mode); err != nil {
port.Close()
return nil, &PortError{code: InvalidSerialPort}
return nil, &PortError{code: InvalidSerialPort, causedBy: fmt.Errorf("error configuring port: %w", err)}
}
unix.SetNonblock(h, false)
@@ -283,7 +284,7 @@ func nativeOpen(portName string, mode *Mode) (*unixPort, error) {
pipe := &unixutils.Pipe{}
if err := pipe.Open(); err != nil {
port.Close()
return nil, &PortError{code: InvalidSerialPort, causedBy: err}
return nil, &PortError{code: InvalidSerialPort, causedBy: fmt.Errorf("error opening signaling pipe: %w", err)}
}
port.closeSignal = pipe