Merge pull request #175 from cmaglie/improve_error_messages

Improved error messages
This commit is contained in:
Cristian Maglie
2024-02-16 14:23:34 +01:00
committed by GitHub

View File

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