Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved error messages #175

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions serial_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package serial

import (
"fmt"
"io/ioutil"
"regexp"
"strings"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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

Expand Down
Loading