From 9aae282f09e92dadad494f174b9b347362c5e9e9 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 16 Feb 2024 14:17:53 +0100 Subject: [PATCH] Improved error messages --- serial_unix.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/serial_unix.go b/serial_unix.go index db6158d..e025d2d 100644 --- a/serial_unix.go +++ b/serial_unix.go @@ -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